弹出键盘时Android Constraint布局底部元素重叠

标签 android android-constraintlayout

在约束布局中,它是一个带有名称、地址、国家、城市、 zip 、州和保存按钮的表单,底部带有 app:layout_constraintBottom_toBottomOf="parent"约束。但是当键盘弹出时,保存按钮与状态输入字段重叠。

按钮 View 应该始终保持在底部,它不能约束到其他 View 。

    <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:id="@+id/c_billing_address_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".activities.subscription.ui.fragments.BillingAddressFragment">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edt_in_name"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_name"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/name_hint_label"
                android:inputType="textPersonName" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edt_in_address"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/edt_in_name">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_address"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/address_hint_label"
                android:inputType="textPostalAddress" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edt_in_country"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edt_in_address">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_country"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/county_hint_label"
                android:inputType="text" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout

            android:id="@+id/edt_in_city"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edt_in_country">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_city"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/city_hint_label" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edt_in_zip"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edt_in_city">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_zip"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/zip_hint_label"
                android:inputType="number" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout

            android:id="@+id/edt_in_state"
            style="@style/Widget.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edt_in_zip">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/edt_state"
                style="@style/Widget.EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/province_hint_label"
                android:inputType="text" />
        </com.google.android.material.textfield.TextInputLayout>

                
        <Button
            android:id="@+id/btn_save"
            style="@style/Widget.Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/save_label"
            app:layout_constraintBottom_toBottomOf="parent" />

        <include layout="@layout/progress_bar_layout" />

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

enter image description here

enter image description here

最佳答案

如上所述,重新排列可滚动和不可滚动的元素,如下所示:

<?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" >

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollViewInitialCompanySelection"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/buttonSave"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayoutInitialCompanySelection"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<!--            <your scrollable views here>-->

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Save &amp; Continue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于弹出键盘时Android Constraint布局底部元素重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63406819/

相关文章:

android - RecyclerView 中具有最大宽度的简单约束布局子项

android - 防止布局被推过特定边界

java - 推荐一个好的Android开发流程

android - 如何直接从screencap.cpp中获取像素数据

android - 文本输入布局颜色

android - 如何从数据库中获取数据并显示在ListView上?

android-如何在谷歌地图上将标记的位置显示为地址

android - ConstraintLayout 是否仍然优于 Jetpack Compose 中的嵌套列和行?

android - constraintlayout.widget.Group 动画不适用于 TransitionManager

android - 在 ConstraintLayout 中对 Google map 进行动画处理会使应用程序卡住