在 ConstraintLayout 中使用 minHeight 查看

View with minHeight in ConstraintLayout

我在 NestedScrollView 里面有一个 ConstraintLayoutConstraintLayout 包含一堆视图,但最后一个 View 可以有一个动态高度来填充底部 space 如果有的话但它也需要一个最小高度如果有的话不够 space.

为了论证,这里有一个例子。

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_height="match_parent">

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintHeight_min="1500dp"
            android:background="@color/red"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
 
    </android.support.constraint.ConstraintLayout>


</android.support.v4.widget.NestedScrollView>

如您所见,我已经放入了 ConstraintLayout 版本,但它不起作用。显然这些值大得离谱,但这只是为了测试。

如果我不在 NestedScrollView 上设置 fillViewport="true",则 ConstraintLayout 的高度为 0。当我设置 fillViewport 时,ConstraintLayout 不滚动,只是填满屏幕。

如何设置视图,使其扩展到 ConstraintLayout 的底部,它应该与视口一样大,但如果我的视图不属于 minHeight,那么我们允许滚动?

我正在使用 ConstraintLayout 库的 1.0.2 版本。

我希望看到的是一直到父级底部的存在,但如果该尺寸小于 1500dp,则视图会滚动。

我像这样输入了 1500dp android:layout_height="1500dp" 并且视图相应地滚动。

更新 1

似乎是我将布局放在 FragmentViewPager 内。 app:layout_constraintHeight_min 属性 不受尊重,它只匹配视口的高度。

我也试过从片段中取出 NestedScrollView 并将 ViewPager 放入其中,但还是没有用。

我正在使用 com.android.support.constraint:constraint-layout:1.0.2,这对我有用:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/gradient"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_min="1500dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>

将此属性添加到您想要拉伸的视图中:

app:layout_constraintHeight_default="spread"

我做了一个小应用程序来演示。没有 java 逻辑可言,但布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:background="#caf">

        <TextView
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:gravity="center"
            android:text="hello world"
            android:background="#fff"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/two"/>

        <TextView
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:gravity="center"
            android:text="hello world"
            android:background="#eee"
            app:layout_constraintTop_toBottomOf="@+id/one"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/three"/>

        <TextView
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="hello world"
            android:background="#ddd"
            app:layout_constraintHeight_default="spread"
            app:layout_constraintHeight_min="300dp"
            app:layout_constraintTop_toBottomOf="@+id/two"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>

底部视图在小于剩余可用空间时会拉伸以填充视口space,并且无法滚动:

底部视图在大于剩余可用高度时保持固定高度space,这使得滚动成为可能:

作为第一件事,我们必须为每个文本视图指定固定高度或使用包装内容作为另一个 option.Secondary 内部约束布局 属性 app:layout_constraintHeight_default="spread" 帮助最后一个视图获得剩余的完整 space 剩余,如果没有 space 剩余则自动同步到滚动视图。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#caf"
        android:padding="16dp">

        <TextView
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:background="#fff"
            android:gravity="center"
            android:text="hello world"
            app:layout_constraintBottom_toTopOf="@+id/two"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:background="#eee"
            android:gravity="center"
            android:text="hello world"
            app:layout_constraintBottom_toTopOf="@+id/three"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/one" />

        <TextView
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:background="#eee"
            android:gravity="center"
            android:text="hello world"
            app:layout_constraintBottom_toTopOf="@+id/four"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/two" />

        <TextView
            android:id="@+id/four"
            android:layout_width="0dp"
            anroid:layout_height="48dp"
            android:background="#eee"
            android:gravity="center"
            android:text="hello world"
            app:layout_constraintBottom_toTopOf="@+id/five"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/three" />

        <TextView
            android:id="@+id/five"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ddd"
            android:gravity="center"
            android:text="hello world"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_default="spread"
            app:layout_constraintHeight_min="300dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/three" />

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>