Android如何填充parent但留下一些space

Android how to fill parent but leave some space

标题不好,不知道怎么形容

我有一个 relative layout,其中包含 2 个元素、一些容器和底部的一个按钮。

我希望容器填满相对布局中的所有 space,仅留下足以放置按钮的 space

这里有一个例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout  //i'm using linearlayout as an example, it could be anything here
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/b3"
        android:gravity="center"
        android:orientation="vertical">


    </LinearLayout>

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="asdasdasdasdasdasdasdasd" />
</RelativeLayout>

这是我想要的样子

红色代表相对布局,绿色代表线性布局,黄色代表按钮...

我怎样才能做到?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/red"
    android:padding="5dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/b"
        android:background="@color/green">

    </LinearLayout>
    <Button
        android:id="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="this is the button"
        android:background="@color/yellow"/>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/b3"
        android:layout_margin="4dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:orientation="vertical">

    </LinearLayout>

    <Button
        android:id="@+id/b3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="asdasdasdasdasdasdasdasd" />
</RelativeLayout>

这应该会给你接近你想要的东西:

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

<LinearLayout  //i'm using linearlayout as an example, it could be anything here
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="3"
    android:gravity="center"
    android:orientation="vertical">
</LinearLayout>

<Button
    android:id="@+id/b3"
    android:layout_width="wrap_content"
    android:layout_height="0px"
    android:layout_weight="1" 
    android:text="asdasdasdasdasdasdasdasd" />

</LinearLayout>