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

3
在 Constraint Layout 中,有一个表单包含姓名、地址、国家、城市、邮编、州和保存按钮,其约束设置为 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

3个回答

2
在项目的AndroidManifest.xml文件中,对应<activity/>标签添加以下属性。
android:windowSoftInputMode="adjustPan"

2

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

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

0

是的,它会重叠,因为您的保存按钮没有与状态字段约束,您的保存按钮父底部约束,所以当您的键盘弹出时,您的父底部键盘的顶部,您应该使用:

app:layout_constraintBottom_toBottomOf="@id/edt_state"

那么它将始终位于state字段下方,同时位于state字段的editText下方。 您还可以在不同的布局中添加此按钮字段,并将其相对于其他布局的bottom设置。


但我总是想把保存按钮放在布局底部。 - Sino
然后,您应该将该按钮放在scrollview之外,显然,然后您需要一个父布局。 - Jyotish Biswas

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