如何在ConstraintsLayout中让View填充剩余空间

34
如何使FrameLayout在ConstraintLayout中填充所有剩余空间?
我有一个xml文件,类似于这样:
<ImageView
    android:background="#FF0000"
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_header"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintWidth_default="spread"/>
<FrameLayout
    android:background="#00FF00"
    app:layout_constraintHeight_default="spread"
    app:layout_constraintWidth_default="wrap"
    app:layout_constraintTop_toBottomOf="@id/header"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@id/footer"
    android:id="@+id/task_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<LinearLayout
    android:background="#FFFF00"
    android:orientation="vertical"
    android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="spread"
    app:layout_constraintWidth_default="spread"
    app:layout_constraintBottom_toBottomOf="parent">
    <ImageButton
        style="?borderlessButtonStyle"
        android:id="@+id/btn_ar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_ar"/>
</LinearLayout>

顶部和底部都有内容,中间有一个FrameLayout,我想让FrameLayout填充所有剩余空间。

我不明白,我该使用哪些属性才能扩展FrameLayout。请帮帮我!

编辑:或者使用不同的布局解决问题。

3个回答

62
这很简单。要使任何视图在水平或垂直方向上填充可用空间,只需根据需要将layout_width / layout_height设置为0dp即可。

13
稍微澄清一下,设置为0dp后,将顶部设置为父视图的顶部或上方视图的底部,并将底部设置为父视图的底部或下方视图的顶部。 - Joseph Sang
2
不错,简洁明了的回答。 - King Of The Jungle

16

这个工作使用ConstraintLayout效果更佳,因为它有更好的性能。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:background="#FF0000"
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_header"
        android:minHeight="30dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintWidth_default="spread"/>

    <FrameLayout
        android:id="@+id/task_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#00FF00"
        android:orientation="vertical"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/btn_ar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintWidth_default="wrap">

    </FrameLayout>

    <ImageButton
        android:id="@+id/btn_ar"
        style="?borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_ar"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

7
<FrameLayout
    android:id="@+id/task_fragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#00FF00"
    android:orientation="vertical"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@+id/btn_ar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="spread"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/header"
    app:layout_constraintWidth_default="wrap">

</FrameLayout>

这应该可以工作。

解释:

你所要做的就是将这个FrameLayout顶部与标题栏底部约束,将此FrameLayout底部与页脚顶部约束。

在设置所有约束之后,您仍然需要设置FrameLayout的布局参数以匹配约束。为了做到这一点,您可以尝试将layout_height设置为0dp。这将使FrameLayout的高度具有match_constraint的效果。

更多细节,请参阅此文档:https://developer.android.com/reference/android/support/constraint/ConstraintLayout(只需搜索关键字match constraint即可找到该块),或者简单地查看此类似的答案:Set width to match constraints in ConstraintLayout


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