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

问题描述 投票:6回答:3

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

 android:layout_alignParentTop="true"

底部设置为......

 android:layout_alignParentBottom="true"

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

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

但后来我需要指定高度和宽度。我应该指定什么样的高度和宽度?

android relativelayout
3个回答
13
投票

这是我的解决方案

<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>

希望这有帮助


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>

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视图。空间是:

轻量级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>
© www.soinside.com 2019 - 2024. All rights reserved.