我有一个相对布局,我正在以编程方式创建:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
现在我有两个TextView,我想在这个相对布局中添加 . 但问题是TextViews显示在RelativeLayout左侧彼此重叠 .
textViewContainer.addView(textView1);
textViewContainer.addView(textView2);
现在我想知道如何以编程方式设置
`android:layout_alignParentRight="true"`
要么
`android:layout_toLeftOf="@id/textView"`
TextViews的属性和我们在xml中的做法一样?
3 回答
您可以使用View.getLayoutParams从代码访问任何LayoutParams . 您只需要非常了解您访问的LayoutParams . 这通常通过检查包含ViewGroup(如果它具有LayoutParams内部子项,然后是's the one you should use. In your case it'的RelativeLayout.LayoutParams)来实现 . 你将使用RelativeLayout.LayoutParams#addRule(int verb)和RelativeLayout.LayoutParams#addRule(int verb, int anchor)
您可以通过代码访问它:
使用方法addrule
您需要将LayoutParams与每个视图对齐:
然后为每个视图添加适当的规则:
到第一个和
到第二个 .
然后将其应用于视图: