安卓 垂直方向上按比例布局

下面是一个小小的例子,我弄了半天才把它弄好的。我要实现的是,上面一个按钮等屏宽,剩下的区域全部给EditText。

有下面几个关键属性要注意。

1、布局容器选的是LinearLayout

2、方向选择为垂直的 android:orientation="vertical"

3、对于两个控件来说, 其layout_width属性就必须设置为match_parent。

4、按钮的android:layout_height设置为wrap_content。

    EditText的layout_height设置为“0dp”,但layout_weight设置为1-------->1指占用剩下高度的所有空间。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Result"
        />

    <EditText
        android:id="@+id/input_message"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>

   实现效果如下:

原文地址:https://www.cnblogs.com/kanite/p/6038923.html