繁体   English   中英

如何以编程方式创建TextView,ImageView和相对布局

[英]How to create TextView,ImageView and Relative Layout Programmatically

我刚出生在android。 我创建了一个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>
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);

更新 https://stackoverflow.com/a/21259802/2793134

您可以使用布局inflater来扩展视图,而不是创建动态视图。 所以你可以使用相同的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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM