32

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

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

我正在努力的部分是为第一个布局(红色)设置最大高度。似乎 ConstraintLayout 忽略了我的“最大高度语句”:

app:layout_constraintHeight_max="300dp"

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

在此处输入图像描述

这是完整的 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>

4

3 回答 3

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

be sure to set the height wrap_content

于 2018-06-01T12:03:30.687 回答
1
<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"
    />

于 2020-04-21T13:46:26.173 回答
0

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

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

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

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

旧答案

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

于 2018-02-20T15:03:16.617 回答