键盘打开时 Android 编辑文本不可见

问题描述

Footer included layout moves up when keyboard is visible

layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activity.LaunchActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true">

        <include
            android:id="@+id/header_relative_layout"
            layout="@layout/header_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <RelativeLayout
            android:id="@+id/body_relative_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/footer_linear_layout"
            android:layout_below="@+id/header_relative_layout"
            android:layout_centerInParent="true" />

        <include
            android:id="@+id/footer_linear_layout"
            layout="@layout/footer_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp" />

    </RelativeLayout>

</RelativeLayout>

我想在键盘可见时向上移动整个布局,但是, 只有页脚布局(包含布局)向上移动。 键盘后面还有两个编辑文本和一个按钮。

android:windowSoftInputMode="adjustResize" 添加到清单活动中。

在 android:id="@+id/body_relative_layout" 中,我使用 layoutInflater 以编程方式添加了布局。

 bodyRelativeLayout.removeAllViews();
  bodyRelativeLayout.addView(bodyView);

解决方法

您可以使用 NestedScrollView 向上滚动整个布局,如下所示:

<RelativeLayout><!--Main Layout-->
 <androidx.core.widget.NestedScrollView>
   <!--Layout code for edittext-->
 </androidx.core.widget.NestedScrollView>

<LinearLayout> 
<! You can directly use include tag here-->
<!--bottom view that will scroll up when keyboard show-->
</LinearLayout>
</RelativeLayout>

示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true"
    android:id="@+id/rlMainLayout">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:isScrollContainer="true"
            android:layout_above="@+id/footer_linear_layout">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                        <include
                            android:id="@+id/header_relative_layout"
                            layout="@layout/header_layout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true" />

                        <RelativeLayout
                            android:id="@+id/body_relative_layout"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_below="@+id/header_relative_layout"
                            android:layout_centerInParent="true" />
                </RelativeLayout>
        </androidx.core.widget.NestedScrollView>
        <include
            android:id="@+id/footer_linear_layout"
            layout="@layout/footer_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp" />
</RelativeLayout>

在清单文件中: android:windowSoftInputMode="adjustResize