约束到底部但允许键盘覆盖视图

constraint to bottom but allow keyboard to cover the view

我有一个视图被限制在父级的底部。我在顶部也有文本字段。如何在键盘出现时将底部视图向上推,从而覆盖我的文本字段。

我的所有内容都在滚动视图中,键盘应该覆盖底部视图,我应该能够滚动到底部以到达底部视图。

这是一个简单的例子。我手动增加了高度以更容易地重现问题。我其实还有更多的看法,这只是为了演示。

请注意,fillViewPort 也已启用。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text_1"
            android:layout_width="0dp"
            android:layout_height="180dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/edit_text_2"
            android:layout_width="0dp"
            android:layout_height="180dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/edit_text_1" />

        <View
            android:id="@+id/bottom_view"
            android:layout_width="0dp"
            android:layout_height="160dp"
            android:background="@color/grey_500"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

您只需要将 ScrollView 放入 ConstraintLayout 像这样:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="0dp"
                android:layout_height="180dp"
                android:inputType="textPersonName"
                android:ems="10"
                android:id="@+id/edit_text_1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="180dp"
                android:inputType="textPersonName"
                android:ems="10"
                android:id="@+id/edit_text_2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/edit_text_1"/>
            <View
                android:layout_width="0dp"
                android:layout_height="160dp"
                android:id="@+id/bottom_view"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/edit_text_2"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>