ConstraintLayout在较大的屏幕上匹配父级高度,但在较小的屏幕上滚动

时间:2018-11-02 16:02:41

标签: android android-layout android-constraintlayout

我已经创建了这样的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="top"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:gravity="center"
        android:minHeight="300dp"
        android:padding="10dp"
        android:text="middle"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="bottom"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

它确实按预期在小屏幕上滚动。 但在较大的屏幕上,它在下方留有空间 enter image description here

如何使布局与屏幕高度匹配,但仍可在小屏幕上滚动?

试图将“中间”高度设置为0dp,但这无济于事。

android:fillViewport="true"添加到ScrollView并将中间位置的高度设置为0dp可以解决空间问题,但会带来滚动问题-在小屏幕上,中间位置正在缩小而不是滚动。

enter image description here

2 个答案:

答案 0 :(得分:1)

将中间TextView的高度设置为0dp(以匹配约束)以填充可用空间,并将android:fillViewport="true"属性添加到ScrollView中以将其内容扩展为填充视口。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
        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">

        <TextView
            android:id="@+id/top"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            android:gravity="center"
            android:padding="10dp"
            android:text="top"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/middle"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/green"
            android:gravity="center"
            android:padding="10dp"
            android:text="middle"
            android:textColor="@color/white"
            app:layout_constraintHeight_min="300dp"
            app:layout_constraintBottom_toTopOf="@id/bottom"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/top" />

        <TextView
            android:id="@+id/bottom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            android:gravity="center"
            android:padding="10dp"
            android:text="bottom"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>

答案 1 :(得分:0)

解决方案是使用app:layout_constraintHeight_min代替android:minHeight

所以这个布局是正确的

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <TextView
        android:id="@+id/top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="top"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/middle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside" />

    <TextView
        android:id="@+id/middle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/green"
        android:gravity="center"
        android:padding="10dp"
        android:text="middle"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="bottom"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/middle" />

</android.support.constraint.ConstraintLayout>