如何使视图填充可用空间?

Gra*_*eme 10 layout android

我知道这一定是一个简单的问题......我只是不知道如何解决它.

所以(只是一个例子),

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:orientation="vertical">
    <Button     android:id="@+id/fakeButton"
                android:layout_height="match_parent"
                android:layout_width="match_parent"/>
    <Button     android:id="@+id/saveSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/saveSearch"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这不起作用 - 第一个按钮会使第二个不可见.权重将使它成为一个百分比游戏,这不是我想要的.

我厚吗?

编辑:我不知道,但似乎顺序很重要(来自答案).加载布局是迭代的而不是整体的.您可以将第一个元素设置为固定高度,其余元素将填充剩余的元素.但我需要的是使最终元素成为固定高度.

Gre*_*ory 11

您可以完全使用layout_weight:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:orientation="vertical">
    <Button     android:id="@+id/fakeButton"
                android:layout_height="1"
                android:layout_width="match_parent"
                android:layout_weight="1.0" />
    <Button     android:id="@+id/saveSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/saveSearch"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有了这个,你就会让底部按钮具有基本高度,而fakeButton会占用所有剩余的空间.

layout_height控制如何在不同视图之间拆分剩余空间.默认值为0(不占用任何额外空间).如果您将两个按钮放在相同的高度并且重量为1.0,那么每个按钮将占用50%的空间.

  • 实际上你需要在2遍中考虑这个系统:首先,忽略权重,正常进行布局.如果LinearLayout中有额外的空间,并且某些孩子的权重> 0,则给予权重> 0的子项额外空间(水平和/或垂直)的百分比,与总权重成比例. (3认同)