Android:RelativeLayout的按钮定位问题

时间:2011-04-09 11:47:42

标签: android alignment relativelayout

我想要这个: B1应在B0之下。 B2应该是B1的权利。 像这样:

B0
B1 B2

但我的代码产生了这个:

B0 B2
B1

这段代码有什么问题?:


RelativeLayout xml_layout = (RelativeLayout) findViewById(R.id.custom_layout_id);

Button[] b_test  = new Button[4];

b_test[1] = new Button(getContext());
b_test[1].setId(1);
b_test[1].setText("B0");
RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(70,70);
lp0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
xml_layout.addView(b_test[1], lp0);

b_test[2] = new Button(getContext());
b_test[2].setId(2);
b_test[2].setText("B1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(70,70);
lp1.addRule(RelativeLayout.BELOW, b_test[1].getId());
xml_layout.addView(b_test[2], lp1);

b_test[3] = new Button(getContext());
b_test[3].setId(3);
b_test[3].setText("B2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(70,70);
lp2.addRule(RelativeLayout.RIGHT_OF, b_test[2].getId());
xml_layout.addView(b_test[3], lp2);

1 个答案:

答案 0 :(得分:1)

添加以下行:

lp2.addRule(RelativeLayout.BELOW, b_test[1].getId());

你应该全力以赴。