ConstraintLayout中的ScrollView无法按预期工作

时间:2019-09-19 07:04:36

标签: android scrollview android-constraintlayout

ConstrainLayout中的ScrollView似乎无法正常工作。 任何面对/尝试过这种xml设计结构的人,请提出建议。

这是xml文件

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".fragment.HomeFragment">
        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollview">

        </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

修改您的<ScrollView>标签以匹配以下内容:

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/cardview">

并将此id属性添加到您的CardView

<androidx.cardview.widget.CardView
    android:id="@+id/cardview"

在不指定底部约束的情况下(并且使用wrap_content作为高度),ScrollView的大小将使其足够大,以使所有内容都适合其内部,但会被父项裁剪ConstraintLayout。如果所有内容都适合,则没有任何可滚动的内容(即使您看不到所有内容,因为父级正在剪切它)。