【问题标题】:Android Edit Text not visible while keyboard is on键盘打开时Android编辑文本不可见
【发布时间】:2021-07-11 05:25:34
【问题描述】:

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" 被添加到 manifest Activity 中。

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

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

【问题讨论】:

    标签: android android-studio android-layout android-softkeyboard


    【解决方案1】:

    您可以使用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

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多