RelativeLayout.BELOW在以编程方式创建的RelativeLayout中不起作用

时间:2015-03-28 10:16:07

标签: android android-layout layout android-relativelayout

我正在以编程方式创建一个包含RelativeLayout和一些TextView的布局。我希望它们像两列一样放置,如下例所示:

textview0  textview1
textview2  textview3
textview4  textview5

并将以下LayoutParameters应用于它们:

width:WRAP_CONTENT

身高:WRAP_CONTENT

和例如textview4的规则:

RelativeLayout.BELOW,textview3.getId()

RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE

和textview5的规则:

RelativeLayout.RIGHT_OF,textview4.getId()

RelativeLayout.ALIGN_BASELINE,textview4.getId()

这可以确保右侧列中的内容可以被包装,但是它下面的文本视图仍然显示正常,因为它位于可以包装的textview下面。

好吧,当我尝试这个时,我让它为前两行textview工作,它们按照它们应该放置,但后来我添加了第三个,并且那个放在同一行应该是放在下面。我无法弄清楚出了什么问题。

RelativeLayout rl = new RelativeLayout(context);
rl.setPadding(5, 5, 5, 5);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp0.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView0.setLayoutParams(lp0);
rl.addView(textView0);

RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.RIGHT_OF, textView0.getId());
lp1.addRule(RelativeLayout.ALIGN_BASELINE, textView0.getId());
lp1.setMargins(5, 0, 0, 0);
textView1.setLayoutParams(lp1);
rl.addView(textView1);

RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, textView1.getId());
lp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView2.setLayoutParams(lp2);
rl.addView(textView2);

RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF, textView2.getId());
lp3.addRule(RelativeLayout.ALIGN_BASELINE, textView2.getId());
lp3.setMargins(5, 0, 0, 0);
textView3.setLayoutParams(lp3);
rl.addView(textView3);

RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.BELOW, textView3.getId());
lp4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView4.setLayoutParams(lp4);
rl.addView(textView4);

RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp5.addRule(RelativeLayout.RIGHT_OF, textView4.getId());
lp5.addRule(RelativeLayout.ALIGN_BASELINE, textView4.getId());
lp5.setMargins(5, 0, 0, 0);
textView5.setLayoutParams(lp5);
rl.addView(textView5);

因此上面的示例代码导致textView4被放置在textView2之上,即使它应该放在它下面,由textView3的LayoutParameters统治。我看不出这个代码有什么问题,我已经调试它以确保ID在textview中是唯一的,它们是。

有任何建议,我有点卡住了!

0 个答案:

没有答案