ConstraintLayout 不遵守最大高度限制。

33
我正在尝试使用ConstraintLayout创建布局组合。为了简化我的情况,我的布局应该有三个部分:
  1. 第一个布局(红色),应根据剩余空间增长并具有最大高度。
  2. 第二个布局(绿色),具有150dp的固定大小,并始终位于第一个布局下方。
  3. 第一个布局(粉色),也具有150dp的固定大小,并应与视图底部对齐。
我遇到的问题是为第一个布局(红色)设置最大高度。似乎ConstraintLayout忽略了我的“最大高度语句”:
app:layout_constraintHeight_max="300dp"

这是我当前的结果(红色部分忽略了高度限制...):

enter image description here

以下是完整的XML:

<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.mixtiles.android.reviewOrder.ReviewOrderActivity"
tools:layout_editor_absoluteY="25dp">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">
        <android.support.v7.widget.Toolbar
            android:id="@+id/review_order_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/red"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/red"
    app:layout_constraintBottom_toTopOf="@+id/green"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_max="300dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
    app:layout_constraintVertical_chainStyle="spread_inside">

</FrameLayout>


<FrameLayout
    android:id="@+id/green"
    android:layout_width="0dp"
    android:layout_height="150dp"
    android:background="@color/greenish"
    app:layout_constraintBottom_toTopOf="@+id/pink"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/red">

</FrameLayout>

<FrameLayout
    android:id="@+id/pink"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/pink"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/green">

</FrameLayout>


我不确定我是否理解正确...粉色始终附着在底部(就像底部导航视图一样),绿色始终位于粉色的正上方,而红色则占据剩余的空间,最多达到300dp? - user11566289
3个回答

62
android:layout_height="wrap_content"
app:layout_constraintHeight_max="300dp"
app:layout_constrainedHeight="true"

一定要将高度设置为wrap_content。


5
对我而言,“app:layout_constrainedHeight="true"”是关键所在。感谢你提供的解决方案! - Randy
1
使用以下代码,我成功地实现了布局:android:layout_height="0dp" app:layout_constraintHeight_max="300dp" - Taras Lozovyi
如果您在layout_height="wrap_content"的情况下使用layout_constraintHeight_max,它实际上不起作用,因为视图大小将会根据其内容进行调整。您应该使用match_parent或其他约束条件,并设置高度约束条件。 唯一适用的情况是资源默认高度实际上比您的最大高度值更大(但对于此情况,您可以直接将高度放入layout_height中)。 - Ionut Negru
非常感谢您提供如此简洁明了的答案,您刚刚救了我的一天! - Megamind Core

2
<View
    android:id="@+id/view2"
    android:layout_width="88dp"
    android:layout_height="0dp"
    android:background="#F44"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintHeight_max="300dp"
    />

<View
    android:id="@+id/view"
    android:layout_width="88dp"
    android:layout_height="150dp"
    android:background="#690"
    app:layout_constraintBottom_toTopOf="@+id/view3"
    app:layout_constraintTop_toBottomOf="@+id/view2"
    />

<View
    android:id="@+id/view3"
    android:layout_width="88dp"
    android:layout_height="150dp"
    android:background="#93C"
    app:layout_constraintBottom_toBottomOf="parent"
    />


1
可能的问题是您不能同时在红色视图上设置这两个约束条件:
app:layout_constraintBottom_toTopOf="@+id/green" app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
如果设置了这两个约束条件,则红色视图将在顶部附加到appBarLayout,在底部附加到绿色视图,这意味着它将占据这两个视图之间的所有空间。
如果您想要遵守最大高度约束,则需要删除这两个约束条件中的一个。应该删除哪一个取决于您期望的最终结果。
旧答案
我认为您应该使用android:maxHeight而不是app:layout_constraintHeight_max。

@RomShiri,请检查我的编辑答案并告诉我它的效果如何。 - papafe
再次感谢,某种程度上它有意义了,但不幸的是,一旦采用其中一个约束条件,红色视图就完全没有高度(使用“匹配约束”)。我真的不明白,如何使用“最大高度”属性..你可以自己尝试,我将示例抽象化,以便您也可以测试它。 - Rom Shiri
@RomShiri,你找到答案了吗?我也遇到了完全相同的问题!我正在考虑将所有内容切换回线性布局。 - rodrigocl
很遗憾,我没有使用ConstratintLayout。相反,我使用了Android Flexbox库,并进行了一些调整以使其适用于我的情况。虽然它不像使用ConstratintLayout那样干净,但至少它能够工作。 - Rom Shiri

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