• 作者:老汪软件技巧
  • 发表时间:2024-10-13 15:03
  • 浏览量:

Android让我的迷惑的windowSoftInputMode

在写项目开发中,我多多少少碰到过键盘和布局打架的场景,每次遇见也不过是去网上搜索一下,复制一下大多数可以解决,但是这一次的情况多少有点特殊,所以就仔细去了解了一下

场景:

我希望达到一个这样子的效果,在一个布局中,输入框和下面所包含的功能菜单在页面的底部,当键盘唤起时,布局被顶上去,和上面的GIF图片一致。

在这个页面中是没有问题的,但是当我切换到另一个页面中出现了另一种情况

发现问题了没?他覆盖了我下面的View,也就是说如果我下面的那4个菜单将会被覆盖(这里只是为了演示,就没用原来的布局了)

我比较好奇原因,由于上面那个情况是没有问题的,我就仔细对比两个布局文件,然后不断调整,发现了问题所在

这个第一个GIF(正常显示)的布局文件

"1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@android:color/transparent"
    android:layout_height="wrap_content">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:id="@+id/moment_List_comment_linear_layout"
        android:background="@drawable/chat_input"
        android:padding="10dp"
        >
        
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:background="@drawable/input"
            android:id="@+id/moment_list_comment_msg_edit"
            />
        <TextView
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:text="@string/send"
            android:background="@drawable/input"
            android:gravity="center"
            android:textSize="15sp"
            android:textColor="@color/teal_700"
            android:id="@+id/moment_list_comment_msg_send_bt"
            />
    LinearLayout>
RelativeLayout>

第二个GIF的布局文件

"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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:id="@+id/moment_List_comment_linear_layout"
        android:background="@drawable/chat_input"
        android:layout_alignParentBottom="true"
        android:padding="10dp"
        >
         
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:background="@drawable/input"
            android:id="@+id/moment_list_comment_msg_edit"
            />
        <TextView
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:text="@string/send"
            android:background="@drawable/input"
            android:gravity="center"
            android:textSize="15sp"
            android:textColor="@color/teal_700"
            android:id="@+id/moment_list_comment_msg_send_bt"
            />
    LinearLayout>
RelativeLayout>

这时候的我还没有在**AndroidManifest.xml对应的Activity中设置windowSoftInputMode**,也就是说,使用RelativeLayout相对布局,将输入框布局固定在屏幕底部,当页面中有滚动布局时,键盘弹出时与window的融合方案是我想要的,但是如果没有滚动的布局,键盘就会定位到输入框的光标处,后面翻阅文档和博客

控制软键盘与视图窗口布局关系有以下几种:参数名称备注作用

adjustResize

调整大小

Activity的主窗口总是调整大小来为软键盘预留空间。如果界面中有可滑动控件,显示效果可能与未指定调整方式类似;如果界面中没有可滑动控件,软键盘可能会盖住一些控件(但布局的位置不会发生变化)

adjustPan

平移

Activity的主窗口并不会调整大小来为软键盘预留空间。相反,当前窗口的内容将会自动平移,以便当前焦点从不被软键盘遮盖。这样用户总能看到输入内容的部分。但需要注意的是,如果用户想与窗口中被遮挡的部分交互,可能需要先关闭软键盘

adjustUnspecified

未指定调整方式

系统的默认设置。没有指明是否减小Activity主窗口的大小来为软键盘预留空间,或者是否当Activity主窗口的部分内容被软键盘遮盖时其内容焦点是可见的。系统会根据窗口内容里是否有可以滑动的布局视图来选择一种模式:如果有这样的视图,Activity的窗口将会调整大小;如果没有,则可能不会调整大小。和布局有很大的关系

adjustNothing

不管

布局不发生任何变化,键盘覆盖在布局上面

控制软键盘可见性的值:参数名称备注作用

stateUnspecified

未指定状态

系统的默认设置。系统会根据界面采取相应的软键盘显示模式。例如,如果界面上只有文本和按钮而没有输入框,软键盘通常不会自动弹出;如果界面上有获取了焦点的输入框,或者界面有滚动需求(如包含RecyclerView、ScrollView等可滑动的控件),软键盘可能会自动弹出

stateUnchanged

状态不改变

当前界面的软键盘状态取决于上一个界面的软键盘状态。如果从上一个界面跳转到当前界面时,软键盘是显示的,那么在当前界面软键盘也是显示的;反之亦然

stateHidden

状态隐藏

不管上一个界面是什么状态,也不管当前界面是否有输入需求,软键盘总是隐藏的

stateAlwaysHidden

总是隐藏

与stateHidden类似,但隐藏范围可能更广,确保软键盘不会显示

stateVisible

通常可见

在适当情况下,软键盘是可见的。当用户导航到该Activity的主窗口时,软键盘可能会自动弹出

stateAlwaysVisible

总是可见

当用户导航到该Activity时,软键盘会自动弹出,即使在界面上没有输入框的情况下也会强制显示

所以按照我的情况,我只需要在第二个页面对应的Activity的清单声明文件中添加上windowSoftInputMode="adjustResize"就好了

效果如下: