【问题标题】:How to set a view's height match parent in ConstraintLayout?如何在 ConstraintLayout 中设置视图的高度匹配父级?
【发布时间】:2018-11-14 11:20:15
【问题描述】:

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

【问题讨论】:

标签: android android-layout android-constraintlayout


【解决方案1】:

解决方案:我假设您将工具栏作为另一个 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" />

然后使用ConstraintLayoutFrameLayout 一起使用,如下所示:

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

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

【讨论】:

    【解决方案2】:

    在 ConstraintLayout 中使用 0dp 即 match_contraint。

    点赞android:layout_height="0dp"

    【讨论】:

    • 感谢@Pankaj 的回答。我已经这样做了,但没有帮助。
    • 不仅android:layout_height="0dp",而且添加app:layout_constraintBottom_toBottomOf="parent"这一行解决了我的问题。
    • @YogeshKatkar 是的,当您使用 ConstraintLayout 时,您需要设置约束。我以为你会做所有这些事情,只有高度不起作用。
    • 我使用了左、右和仅顶部,缺少底部约束。
    【解决方案3】:

    给工具栏一个 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>
    

    【讨论】:

      【解决方案4】:

      注意,与 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>
      

      【讨论】:

        猜你喜欢
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        相关资源
        最近更新 更多