如何通过编程动态创建 TextView、ImageView 和 RelativeLayout

3

我是一个新手安卓开发者。我创建了一个XML文件,但现在我想通过编程方式创建所有的布局。我看到了很多例子,但我找不到我的解决方案。

我想以编程方式创建以下这些组件:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:paddingTop="10dp"
            android:paddingLeft="60dp"
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bag" />


        <TextView
            android:paddingTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal"
            android:layout_toEndOf="@+id/imageView1"
            android:layout_toRightOf="@+id/imageView1"
            android:gravity="center_vertical"
            android:onClick="bugAndLuggage"
            android:text="@string/bug_luggage"
            android:paddingLeft="10dp"/>


    </RelativeLayout>

为什么要动态创建视图?你可以扩展相同的视图啊。 - droidev
2个回答

0
RelativeLayout relativeLayout=new RelativeLayout(context);
//set Relative Layout Parameter using 
RelativeLayout.LayoutParams relativeLayoutParams=new RelativeLayout.LayoutParams(RelativeLayout.MATCH_PARENT,RelativeLayout.MATCH_PARENT);
relativeLayout.setLayoutParams(relativeLayoutParams);

//以同样的方式创建ImageView

ImageView imageView=new ImageView(context);
relativeLayout.addView(imageView);

// 将布局参数添加到每个视图中,使用在xml中设置的不同选项

TextView textView=new TextView(context);
relativeLayout.addView(textView);

UPDATE https://stackoverflow.com/a/21259802/2793134


еҰӮдҪ•и®ҫзҪ®TextViewзҡ„еұһжҖ§android:layout_toEndOf="@+id/imageView1" android:layout_toRightOf="@+id/imageView1"гҖӮ - user5599808

-1

不必创建动态视图,您可以使用布局填充器来膨胀您的视图。 这样,您可以使用相同的XML布局文件来填充视图。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:paddingTop="10dp"
        android:paddingLeft="60dp"
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bag" />


    <TextView
        android:paddingTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textColor="@android:color/black"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:layout_toEndOf="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:gravity="center_vertical"
        android:onClick="bugAndLuggage"
        android:text="@string/bug_luggage"
        android:paddingLeft="10dp"/>


</RelativeLayout>

和在代码中

   View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
   RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.srelative_layout_id);
   TextView tv= (TextView)v.findViewBytId(R.id.id_of_text_view); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接