FrameLayout:填充空白空间

4

我在布局中有3个FrameLayout,第一个和第三个有固定高度(子视图的wrap_content)。

我希望第二个FrameLayout填充第一个和第三个元素之间的剩余空间。

我的XML代码如下:

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

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <!-- ... childs views with fixed height -->
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" <!-- Fill remaining space -->
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp" >

    <!-- ... childs views with match_parent height -->
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <!-- ... childs view with fixed height -->
</FrameLayout>

</LinearLayout>

在这种情况下最好使用RelativeLayout作为父级。 - JiTHiN
你能解释一下为什么吗? - Florian Mac Langlade
如果您使用RelativeLayout,则可以将0dp的FrameLayout声明为最后一个,并给它match_parent,以填充剩余的高度。 - Phantômaxx
你可以使用FrameLayout做同样的事情,请看@blackbelt的回答 :) - Florian Mac Langlade
1个回答

4

您可以使用layout_weight="1"属性用于中央的FrameLayout,将height设置为0dip。它将占据在其他两个之后剩余的空间。


Android Studio 抱怨“嵌套权重会影响性能”。 还有其他解决方案吗? - Hamid Z
你可以尝试使用ConstraintLayout,并将四个约束条件start/end/top/bottom设置为parent。这只是最终的警告。 - Blackbelt

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