首页 > 解决方案 > 仅为一个 ConstraintLayout 链视图设置最大高度

问题描述

我在链式垂直链中有两个ConstraintLayout视图spread_inside。我只想layout_constraintHeight_max为第一个视图设置最大高度,但由于它是链的头部,它适用于所有链式视图。

如何仅将最大高度约束应用于链的第一个视图?

标签: androidandroid-constraintlayout

解决方案


的这种行为layout_constraintHeight_max是违反直觉的。尝试以下布局,它应该模拟一个没有烦人layout_constraintHeight_max行为的链。它使用屏障来标记底部视图的顶部。

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/topView"
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toTopOf="@id/barrier"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="200dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="This is the top view." />

    <TextView
        android:id="@+id/bottomView"
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/topView"
        tools:text="This is the bottom view." />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="top"
        app:constraint_referenced_ids="bottomView" />
</androidx.constraintlayout.widget.ConstraintLayout>

你说顶视图可以缩小到0dp。如果您的意思是零高度,那么上述内容本身可能不会那么远。


推荐阅读