[英]Dynamically adding component in relative layout
I have an xml structure like, 我有一个xml结构,
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:minWidth="20dp"
android:text="05"
android:background="@drawable/rounded_corners"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txt2"
style="android:textViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:layout_toRightOf="@+id/txt1"
android:background="@drawable/rounded_corners"
android:minWidth="20dp"
android:text=""
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:layout_toRightOf="@+id/txt2"
android:background="@drawable/rounded_corners"
android:minWidth="20dp"
android:text=""
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
As I need this TextView to be dynamically added to View. 因为我需要将此TextView动态添加到View。 I am trying to convert same in onCreate() method like below,
我试图在如下的onCreate()方法中进行相同的转换,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable rounded_corners = getResources().getDrawable(R.drawable.rounded_corners);
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.mainrelativeactivity);
TextView tv = new TextView(this);
RelativeLayout.LayoutParams rel = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(rel);
tv.setText("ABC");
tv.setBackground(rounded_corners);
relativeLayout.addView(tv);
tv = new TextView(this);
rel = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(rel);
tv.setText("DEF");
tv.setBackground(rounded_corners);
relativeLayout.addView(tv);
}
But I don't understand how to set layout_alignParentLeft
, layout_marginTop
and most concern is layout_toRightOf
part? 但是我不明白如何设置
layout_alignParentLeft
, layout_marginTop
和最关心的是layout_toRightOf
部分?
If some one helpful to direct me to example like this then it will be great... 如果有人帮我指导这样的例子,那就太好了...
You'll have to add the rule with the RelativeLayout.LayoutParams
to accomplish what u want.. 您必须添加带有
RelativeLayout.LayoutParams
的规则才能完成您想要的操作。
Something like this 像这样
For aligning a view to right of an another view 用于将视图对齐到另一个视图的右侧
rel.addRule(RelativeLayout.RIGHT_OF, tv.getId());
For aligning a view to the left in parent 用于将视图在父视图中向左对齐
rel.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
For giving margins 为了给利润
rel.setMargins(50, 10, 0, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.