android - 在 ConstraintLayout 中使用 minHeight 查看

标签 android android-layout android-constraintlayout

我在 NestedScrollView 中有一个 ConstraintLayoutConstraintLayout 包含一堆 View ,但最后一个 View 可以有一个动态高度来填充底部空间(如果有)但它也需要一个最小高度,如果没有足够的空间。

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

<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。当我设置时fillViewportConstraintLayout 不滚动,只是填满屏幕。

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

我正在使用 ConstraintLayout 库的版本 1.0.2

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

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

更新 1

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

我还尝试从 fragment 中取出 NestedScrollView 并将 ViewPager 放入其中,但还是没有用。

最佳答案

将此属性添加到您想要拉伸(stretch)的 View 中:

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>

当底部 View 小于剩余可用空间时,底部 View 会拉伸(stretch)以填充视口(viewport),并且无法滚动:

enter image description here

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

enter image description here enter image description here

关于android - 在 ConstraintLayout 中使用 minHeight 查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46654512/

相关文章:

Android TabWidget检测点击当前标签

android - 里面有文字的图标 - 如何在布局中定义?

android - ConstraintLayout 中的组阻止设置 ProgressBar 可见性

android - 底部按钮

android - 当有很多 subview 时,如何使相对布局可滚动?

android - 如何在 ConstraintLayout 中不使用 margin 属性的情况下设置两个 View 之间相对于屏幕宽度的空间

Android 以编程方式添加带有 ConstraintLayout 的 ImageView

android - ListView 滚动时隐藏软输入

java - Android开发-内部类中的变量

android - 如何在android中设置listview特定行的背景?