Android:将视图放在与其他视图相同的高度

时间:2012-11-17 18:28:03

标签: java android android-layout layout relativelayout

我有一个LinearLayout(垂直水平),包含2个RelativeLayouts,每个包含1个按钮。 在左侧RelativeLayout中,按钮以父级为中心。在右侧RelativeLayout中,有一个稍大的按钮。 我希望那个更大的按钮的底部与另一个按钮的高度相同。像这样:

enter image description here

你知道我怎么能做到这一点吗?

非常感谢提前

2 个答案:

答案 0 :(得分:1)

在两个按钮上使用android:layout_alignParentBottom,然后在两个按钮上应用相同值的android:layout_marginBottom。

答案 1 :(得分:1)

分别使用 onWindowFocusChanged 中的getBottom()和getWidth获取较小按钮的底部位置和宽度。

在运行时创建另一个按钮。将top padding设置为第二个相对布局,作为button1的bottom_position - height_of_button2。

在设置所需的高度和宽度(从butoon1的getWidth获得)后,将此按钮添加到第二个相对布局。

以下是样本: -

public void onWindowFocusChanged(boolean hasFocus) {
        relative_layout_two = (RelativeLayout) findViewById(R.id.rl2);
        int bottom_position_of_button1 = bt1.getBottom();
        int width_of_button1 = bt1.getWidth();
        bt2 = new Button(getApplicationContext());
        rl.setPadding(0, bottom_position_of_button1-200, 0, 0);// I have taken 200 as the height of second button
        rl.addView(bt2, width_of_button1, 200);

    };

希望这有帮助!