ConstraintLayout不尊重最大高度

Rom*_*iri 16 android android-constraintlayout constraint-layout-chains

我正在尝试使用ConstraintLayout创建布局组合.为了简化我的案例,我的布局应该有三个部分:

  1. 第一个布局(红色),应根据剩余空间增长并具有最大高度.
  2. 第二个布局(绿色),固定大小为150dp,应始终低于第一个布局.
  3. 第一个布局(粉红色),也有150dp的固定大小,应该与视图底部对齐.

我正在努力的部分是为第一个布局设置最大高度(红色).好像ConstraintLayout忽略了我的"最大高度语句":

app:layout_constraintHeight_max="300dp"
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

这是完整的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>
Run Code Online (Sandbox Code Playgroud)

小智 23

android:layout_height="wrap_content"
app:layout_constraintHeight_max="300dp"
app:layout_constrainedHeight="true"
Run Code Online (Sandbox Code Playgroud)

一定要设置高度wrap_content

  • app:layout_constrainedHeight="true" 对我来说是不同的。感谢您的解决方案! (2认同)

mar*_*ian 1

问题可能是您无法在红色视图上同时设置这两个约束:

app:layout_constraintBottom_toTopOf="@+id/green" app:layout_constraintTop_toBottomOf="@+id/appBarLayout"

如果设置了这两个约束,那么红色视图将附加到 appBarLayout 的顶部,底部附加到绿色视图,这意味着它将占据这两个视图之间的所有空间。

如果您希望遵守最大高度约束,则需要删除这两个约束之一。应该删除哪一个取决于您期望的最终结果。

旧答案

我认为你应该使用android:maxHeight而不是app:layout_constraintHeight_max.