如何在parent相对布局中填充所有space?

How to fill all space in parent Relative layout?

我创建了一个小例子来解释我的问题。在示例中,我创建了一个填满整个屏幕的 RelativeLayout。和 3 children :

  1. FrameLayout with id middle that must fill all emptyspace FrameLayout 的顶部和底部没有覆盖(粉红色)
  2. id 为 top 的 FrameLayout 必须显示在中间上方(红色)
  3. 带有 id 底部的 FrameLayout 必须显示在中间下方(黄色)

布局:

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

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_above="@id/middle"
        android:background="#FFFF0000" />

    <FrameLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />   

    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_below="@id/middle"
        android:background="#FFFFFF00" />    

</RelativeLayout>

我的示例结果截图:

以及我需要做的截图:

希望对您有所帮助

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

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFF0000"
        android:layout_alignParentTop="true"
        />

    <FrameLayout
        android:id="@+id/middle"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />
    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFFFF00"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

像这样的东西应该可以工作:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/middle"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFF0000" />

    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_gravity="bottom"
        android:background="#FFFFFF00" />

</FrameLayout>

您可以使用 LinearLayout 而不是使用 RelativeLayout 并使用 layout_weight 来填充剩余的 space.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/top"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFF0000" />

<FrameLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#FFFF00FF"
    android:layout_weight="1"/>

<FrameLayout
    android:id="@+id/bottom"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFFFF00" />

</LinearLayout>

要使用相对布局做到这一点,您需要将顶部和底部布局与边框对齐,然后设置它们之间的中间布局。

见下文:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="pl.orbitemobile.myapplication.MainActivity">
    
    
            <FrameLayout
                android:id="@+id/top"
                android:layout_width="100dp"
                android:layout_height="24dp"
                android:layout_alignParentTop="true"
                android:background="#FFFF0000" />
    
            <FrameLayout
                android:id="@+id/middle"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/top"
                android:layout_above="@+id/bottom"
                android:background="#FFFF00FF" />
    
            <FrameLayout
                android:layout_width="100dp"
                android:id="@+id/bottom"
                android:layout_height="24dp"
                android:layout_alignParentBottom="true"
                android:background="#FFFFFF00" />
    
</RelativeLayout>

效果:

好的,这段代码应该可以做到。

看看:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/top"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFF0000"
    android:layout_alignParentTop="true" />
<FrameLayout
    android:id="@+id/bottom"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFFFF00"
    android:layout_alignParentBottom="true" />
<FrameLayout
    android:id="@+id/middle"
    android:layout_above="@id/bottom"
    android:layout_below="@id/top"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FFFF00FF" />
</RelativeLayout>