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

5

我有一个被限制在父视图底部的视图。我还有置于其上方的文本字段。但是当键盘弹出时,它会将底部视图向上推,覆盖我的文本字段。

我将所有内容放在滚动视图中,键盘应该遮盖底部视图,并且我应该能够滚动到底部以查看底部视图。

这里是一个简单的示例。我手动增加了高度以更轻松地重现问题。实际上我有更多视图,这只是为了演示。

请注意,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>
1个回答

2
你只需要将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>

实际上,layout_constraintVertical_bias 就是解决问题的关键!感谢您的回答。但是,您的回答目前是错误的。在 ConstraintLayout 中不必将 ScrollView 放置在其中。您还使用了 layout_constraintHorizontal_bias 而不是垂直方向的偏移量。尽管如此,非常感谢! - M.kazem Akhgary
你的更新对我不起作用。我正在API 26上进行测试。但是将垂直偏移设置为1却非常有效。 - M.kazem Akhgary
很有趣,我也在API 26上进行了测试,它完美地运行了。 - Matt
我可能漏掉了什么。你的答案很好,但是当键盘隐藏时,bottom_view不在父视图的底部。如果你为bottom_view设置Bottom_toBottomOf="parent"并且同时设置layout_constraintVertical_bias="1.0",它就会按照我想要的方式工作。 - M.kazem Akhgary
你可能想要给它一个黑色背景,这样你就可以看到它的位置。 - M.kazem Akhgary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接