【问题标题】:constraint to bottom but allow keyboard to cover the view约束到底部但允许键盘覆盖视图
【发布时间】:2018-12-14 17:32:12
【问题描述】:

我有一个限制在父级底部的视图。我也有顶部的文本字段。当键盘出现时,它会将底部视图向上推,这将覆盖我的文本字段。

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

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

请注意,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>

【问题讨论】:

    标签: android scrollview android-softkeyboard android-constraintlayout


    【解决方案1】:

    您只需将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 成功了!感谢您的回答。但你的答案目前是错误的。没有必要将滚动视图放在约束布局中。您还使用了layout_constraintHorizontal_bias 而不是垂直的。不过还是非常感谢!
    • 您的更新对我不起作用。我正在 API 26 上对此进行测试。但是将垂直偏差设置为 1 就像一个魅力。
    • 有意思,我也在 API 26 上测试过,效果很好
    • 我可能错过了一些东西。你的答案很好,但是当键盘隐藏时bottom_view 不在父级的底部。如果您设置Bottom_toBottomOf="parent" 并为bottom_view 设置layout_constraintVertical_bias="1.0",它会按照我想要的方式工作。
    • 你可能想给一个黑色的背景,这样你就可以看到它的位置。
    猜你喜欢
    • 2018-03-12
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多