如何在RelativeLayout中填充两个视图之间的空间

8

我有两个视图。顶部视图设置为...

 android:layout_alignParentTop="true"

底部被设置为...
 android:layout_alignParentBottom="true"

我该如何用第三个视图填充剩余的空间?根据这个回答(链接),我应该像这样使用框架布局...

<FrameLayout 
   android:layout_below="@+id/toplayout" 
   android:layout_above="@+id/bottomlayout"/>

但是我需要指定高度和宽度。我应该指定什么高度和宽度?


我认为在两者中使用match_parent就可以了。 - Mike M.
哦,我刚刚测试了一下,对我来说它按预期工作。 - Mike M.
我认为在技术上使用0dp作为高度更加高效,但是两种方式都可以。 - Kevin Coppock
实际上,至少对于常规的视图来说,无论指定了什么高度,它似乎仍然有效。你所包含的布局只是一个ViewPager吗? - Mike M.
你说得对,它正在工作。只需要应用一些填充,并意识到从一开始就没有任何重叠... - the_prole
显示剩余2条评论
3个回答

18

这是我的解决方案

<RelativeLayout
     ...
   >
        <YourLayout
         android:id="@+id/toplayout"
         android:layout_alignParentTop="true"
        />

        <YourLayout
         android:id="@+id/bottomlayout"
         android:layout_alignParentBottom="true"
        />

        <MiddleLayout 
          <!-- in your case it is FrameLayout -->
           android:layout_width="match_parent"
           android:layout_height="match_parent" 
          <!-- or android:layout_height="wrap_content" according to the_profile -->
           android:layout_below="@+id/toplayout" 
           android:layout_above="@+id/bottomlayout"/>
    </RelativeLayout>

希望这可以帮到您


1
非常感谢,我已经将它添加到我的答案中,希望能帮助其他人。 - Linh
1
完美运作,注意不要出现循环引用,例如将layout_below设置为底部元素e,将layout_above设置为中间元素。 - David Clifte
我可以使用约束布局来实现吗? - William

2
只要您的根布局是一个RelativeLayout,并且您在顶部视图上使用layout_alignParentTop,在底部视图上使用layout_alignParentBottom,如您所述,那么就应该不需要中间视图就可以工作:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

另外,如果您的根视图是LinearLayout,您可以使用一个较少人知道但很合适的名为Space的视图。Space是:

一个轻量级的View子类,可以用于在通用布局中创建组件之间的空白。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

2
你可以利用权重的优势。 android:layout_weight 属性通常填充剩余的空间(或平均分配)。在你的情况下,它可能是这样的:
<LinearLayout 
   android:layout_width="match_parent"
   android:layout_height="some_fixed_height"
   android:orientation="vertical">

    <LinearLayout
            android:id="@+id/top_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

    <LinearLayout
            android:id="@+id/middle_one_that_fills"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">


    <LinearLayout
            android:id="@+id/bottom_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">   

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接