使用 RelativeLayout 使 View 占用所有剩余 space

Using RelativeLayout to make a View take all remaining space

我想实现这个

我正在做这个

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/upper"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/wallet_dim_foreground_inverse_disabled_holo_dark">
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:layout_below="@+id/upper"
        android:background="@color/blueAccentColor">
    </LinearLayout>
</RelativeLayout>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/upper"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/lower"
        android:background="@color/wallet_dim_foreground_inverse_disabled_holo_dark">
        </LinearLayout>
    <LinearLayout
        android:id="@+id/lower"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="@color/blueAccentColor"></LinearLayout>
    </RelativeLayout>

您必须将第一个布局设置在第二个布局之上。

使用您提供的代码,您将拥有一个占据所有 space 的顶部布局,以及一个位于他下方的底部布局。如果您考虑一下,您会发现结果会有所不同是合乎逻辑的。

如果您从下方移除 android:layout_below="@+id/upper" 并添加 android:layout_alignParentBottom="true",此布局将固定在底部,无论屏幕大小如何。

比起将 android:layout_above="@+id/lower" 添加到顶部布局,他将拿走所有 space(感谢 android:layout_height="fill_parent"),但它将保留在较低布局之上。

这是你想要的结果:)