布局视图中RelativeLayout的编程视图、布局、RelativeLayout

2023-09-11 11:01:04 作者:星星曲奇

我想才达到以下编程方式(而不是通过XML声明):

 < RelativeLayout的...>
   < TextView中...
      机器人:ID =@ + ID / label1的/>
   < TextView中...
      机器人:ID =@ + ID / LABEL2
      机器人:layout_below:@用户名/卷标1/>
< / RelativeLayout的>
 

在换句话说,如何让我的第二个TextView的出现下面的第一个,但我想这样做在code:

  RelativeLayout的布局=新RelativeLayout的(这一点);
TextView的卷标1 =新的TextView(本);
TextView的标签2 =新的TextView(本);
...
layout.addView(LABEL1);
layout.addView(LABEL2);
的setContentView(布局);
 

更新:

谢谢,TreeUK。我理解的大方向,但它仍然无法正常工作 - B重叠A。我究竟做错了什么?

  RelativeLayout的布局=新RelativeLayout的(这一点);
TextView的TV1 =新的TextView(本);
tv1.setText(A);

TextView的TV2 =新的TextView(本);
tv2.setText(B);
RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF,tv1.getId());

layout.addView(TV1);
layout.addView(TV2,LP);
 
Android4种布局详解

解决方案

从我已经能够拼凑,你必须使用的LayoutParams添加视图。

 的LinearLayout的LinearLayout =新的LinearLayout(本);

RelativeLayout.LayoutParams relativeParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

parentView.addView(LinearLayout中,relativeParams);
 

所有信贷sechastain,编程,你必须IDS分配给他们相对定位您的项目。

  TextView的TV1 =新的TextView(本);
tv1.setId(1);
TextView的TV2 =新的TextView(本);
tv2.setId(2);
 

然后 addRule(RelativeLayout.RIGHT_OF,tv1.getId());

I'm trying to achive the following programmatically (rather than declaratively via XML):

<RelativeLayout...>
   <TextView ...
      android:id="@+id/label1" />
   <TextView ...
      android:id="@+id/label2"
      android:layout_below: "@id/label1" />
</RelativeLayout>

In other words, how do I make the second TextView appear below the first one, but I want to do it in code:

RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);

Update:

Thanks, TreeUK. I understand the general direction, but it still doesn't work - "B" overlaps "A". What am I doing wrong?

RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");

TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());

layout.addView(tv1);        
layout.addView(tv2, lp);

解决方案

From what I've been able to piece together, you have to add the view using LayoutParams.

LinearLayout linearLayout = new LinearLayout(this);

RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

parentView.addView(linearLayout, relativeParams);

All credit to sechastain, to relatively position your items programmatically you have to assign ids to them.

TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);

Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());