弹出键盘时,Android约束布局底部元素会重叠

问题描述

在约束布局中,其形式为名称,地址,国家,城市,邮政编码,状态和保存按钮,底部带有app:layout_constraintBottom_toBottomOf =“ parent”约束。但是,当键盘弹出时,“保存”按钮会与状态输入字段重叠。

按钮视图应始终停留在底部,不能约束其他视图。

    <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

解决方法

是的,这会重叠,因为您的save button不受state field约束,您的save buttonparent bottom约束,所以当您的keyboard弹出时,您的{ {1}}是您应使用的parent bottom的顶部:

keyboard

然后,它将始终在该app:layout_constraintBottom_toBottomOf="@id/edt_state" 字段下以及state字段的editText下。 您还可以将按钮字段添加到其他布局中,并将其state与其他布局相对。

,

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

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

在AndroidManifest.xml文件中对应的<activity/>标签中添加如下属性

android:windowSoftInputMode="adjustPan"