如何在ConstraintLayout中设置视图的高度为match_parent?

4

我正在创建一个布局。我想将自定义的工具栏(另一个布局)设置在布局的顶部,并将FrameLayout放置在工具栏下面。但是FrameLayout应该获取布局剩余的高度(match_parent)。如何使用约束布局实现��一点?


请查看我的解决方案,并检查它是否完全符合您的需求。 - Ümañg ßürmån
4个回答

12

解决方案:我假设您的工具栏是另一个XML文件,类似于以下内容:

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="60dp"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

然后使用 ConstraintLayout 将其与 FrameLayout 结合使用,如下所示:

<?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"
    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</android.support.constraint.ConstraintLayout>

希望这可以帮到你。如果你有任何问题,请在评论区留言。


11

在 ConstraintLayout 中使用 0dp,它相当于 match_constraint。

例如:android:layout_height="0dp"


感谢@Pankaj的回答。我已经尝试过了,但没有帮助。 - Yogesh Katkar
4
不仅仅是将 android:layout_height="0dp",还需要加上 app:layout_constraintBottom_toBottomOf="parent" 这行代码才能解决我的问题。 - Yogesh Katkar
@YogeshKatkar 是的,因为您正在使用ConstraintLayout,所以需要设置约束条件。我认为您会做所有这些事情,只有高度不起作用。 - Pankaj Kumar
我使用了左、右和上的约束,只是底部的约束缺失了。 - Yogesh Katkar

1

给工具栏一个ID,在帧布局中将宽度和高度设置为0dp,并添加像这段代码中的约束。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Main2Activity">

    <Toolbar
        android:id="@+id/toolbar"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#ab1010"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </Toolbar>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="#f1f1df"
        >

    </FrameLayout>


</android.support.constraint.ConstraintLayout>

0
请注意,与 Android 中的其他视图组不同,ConstraintLayout 的子视图之间应该存在循环依赖关系。因此,您应该为每个子视图使用 0dp 而不是“match_parent”宽度,并为每个边缘设置约束条件。
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

</android.support.constraint.ConstraintLayout>

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