如何在滚动视图内的约束布局中在滚动视图外插入固定页脚

How insert fixed footer outside scroll view in constrain layout inside scrollview

我想在屏幕底部插入固定的页脚,上面应该是我在下面代码中使用的滚动视图中的内容,但它抛出异常ScrollView can host only an direct child

<layout
    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">
    <data>
        <variable
            name="mainActivity"
            type="com.example.footer.MainActivity" />
    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </ScrollView>

    <TextView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

主要问题是约束设置不正确,需要更改 ScrollView layout_height 和 id。

ScrollView 需要一个 ID android:id="@+id/scrollView" 底部约束位于 TextView layout_constraintBottom_toTopOf="@+id/footer"

的顶部

TextView 需要顶部约束才能成为 ScrollView layout_constraintTop_toBottomOf="@+id/scrollView"

的底部

另一个问题是 ScrollView 的高度需要使用 "match_constraint",在布局的代码视图中是这样设置的:android:layout_height="0dp"

When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.

这是根据上述建议更新后的最终布局:

<layout
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">
<data>
    <variable
        name="mainActivity"
        type="com.example.footer.MainActivity" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/footer">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    </ScrollView>

    <TextView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

试一试,可能就是您要找的

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

<!--        ScrollView can only contain 1 view inside-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/footer"
            app:layout_constraintTop_toTopOf="parent">

<!--            Use LinearLayoutCompat or ConstraintLayout or layout any-->
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            <!-- do some thing -->
            </androidx.appcompat.widget.LinearLayoutCompat>

        </ScrollView>

        <TextView
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>