使用 ViewPager 填充剩余屏幕 space

Fill left over screen space using ViewPager

我正在尝试制作一个 ViewPager 垂直填充可用 space 的布局。

基本上我这里有一堆垂直堆叠的视图。

Checkbox
ViewPager
LinearLayout (which will contain the indicator)
Button

我想要顶部的复选框,底部的按钮,按钮上方的 LinearLayout,以及中间留下的任何 space 供 ViewPager 使用,但是我不能似乎能够让它发挥作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white"
    android:gravity="top|center_horizontal"
    android:padding="@dimen/activity_horizontal_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:background="@drawable/login_button"
        android:id="@+id/checkbox_friend_notify"
        android:visibility="invisible"
        android:checked="false"
        android:textColor="@color/black"
        android:text="@string/friend_notify"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ViewPager
        android:id="@+id/pager"
        android:gravity="top"
        android:layout_below="@+id/checkbox_friend_notify"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/pager_indicator"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_below="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/start_btn"
        android:visibility="invisible"
        android:text="@string/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@drawable/login_button"
        android:layout_below="@+id/pager_indicator"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

有什么指点吗?

如果您只需要一组垂直堆叠的视图,请考虑将您的 RelativeLayout 更改为垂直 LinearLayout。然后你可以保持你所有的 child 视图相同(摆脱所有布局 above/below 属性)除了给 ViewPager android:layout_height="0dp"android:layout_weight="1"

解释: layout_weight 告诉 LinearLayout 如何分配剩余的白色 space,并且通过给 ViewPager 一个权重(而不给任何其他 children 任何权重值)你告诉 LinearLayout ViewPager想要所有剩余的白色 space 给自己。这仅在您未定义高度时有效(因此 layout_height=0dp)。

您可以在 LinearLayout guide 上阅读更多相关信息。