【问题标题】:Android: Button Positioning problem with RelativeLayoutAndroid:RelativeLayout 的按钮定位问题
【发布时间】:2011-08-02 01:37:46
【问题描述】:

我想要这个: 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);

【问题讨论】:

  • 只需添加 lp2.addRule(RelativeLayout.BELOW, b_test[1].getId());

标签: android alignment android-relativelayout


【解决方案1】:

添加以下行:

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

你应该准备好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2018-07-31
    • 2019-10-07
    相关资源
    最近更新 更多