[英]How to create TextView,ImageView and Relative Layout Programmatically
I am new born in android. 我刚出生在android。 I created a xml file but now i want to create all my layout programmatically. 我创建了一个xml文件,但现在我想以编程方式创建我的所有布局。 I see many examples but i couldn't find my solution. 我看到很多例子,但我找不到我的解决方案。
I want below these component programmatically same :- 我想在编程相同的下面这些组件: -
<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);
//Create ImageView in the same manner //以相同的方式创建ImageView
ImageView imageView=new ImageView(context);
relativeLayout.addView(imageView);
// add layout parameters to each view with different options as you //have set it in the xml //在每个视图中添加布局参数,并在xml中设置不同的选项
TextView textView=new TextView(context);
relativeLayout.addView(textView);
UPDATE https://stackoverflow.com/a/21259802/2793134 更新 https://stackoverflow.com/a/21259802/2793134
instead of creating Dynamic views you can use layout inflater to inflate your view. 您可以使用布局inflater来扩展视图,而不是创建动态视图。 so you can use the same xml layout file for inflating the views. 所以你可以使用相同的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>
and in code 并在代码中
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.