如何在 ConstraintLayout 中设置视图的高度匹配父级?

Yog*_*kar 0 android android-layout android-constraintlayout

我创建一个layout。我想设置我的自定义toolbar(这是另一种布局)到顶部layoutFrameLayout正下方toolbar。但是FrameLayout应该得到layout的高度的其余部分(match_parent)。如何使用约束布局来实现这一点?

Üma*_*mån 12

解决方案:我假设您将工具栏作为另一个 xml 文件,如下所示:

工具栏.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" />
Run Code Online (Sandbox Code Playgroud)

然后像这样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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。如果您有任何问题,请发表评论。


Pan*_*mar 7

0dp在 ConstraintLayout 中使用match_contraint。

喜欢 android:layout_height="0dp"

  • 不仅 `android:layout_height="0dp"` 而且添加 `app:layout_constraintBottom_toBottomOf="parent"` 这一行解决了我的问题。 (4认同)