仅为一个 ConstraintLayout 链视图设置最大高度

Setting max height for only one ConstraintLayout chain view

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

如何只对链条的第一个视图应用最大高度限制?

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。如果您指的是零高度,那么上述内容本身可能不会走那么远。