如何铺陈在RelativeLayout的意见编程? [英] How to lay out Views in RelativeLayout programmatically?

查看:234
本文介绍了如何铺陈在RelativeLayout的意见编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现编程以下(而不是通过声明XML):

 < RelativeLayout的...>
   < TextView的...
      机器人:ID =@ + ID / LABEL1/>
   < TextView的...
      机器人:ID =@ + ID / LABEL2
      机器人:layout_below:@ ID / LABEL1/>
< / RelativeLayout的>

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

  RelativeLayout的布局=新的RelativeLayout(本);
TextView的LABEL1 =新的TextView(本);
TextView的LABEL2 =新的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);


解决方案

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

 的LinearLayout的LinearLayout =新的LinearLayout(本);RelativeLayout.LayoutParams relativeParams =新RelativeLayout.LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);parentView.addView(LinearLayout中,relativeParams);

所有信贷sechastain,相对定位您的项目编程,你必须ID指派给他们。

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

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

I'm trying to achieve 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.MATCH_PARENT, LayoutParams.MATCH_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());

这篇关于如何铺陈在RelativeLayout的意见编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆