Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

东方客主
• 阅读 1549

问题

1、页面布局文件:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_order_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"> 

2、配置文件不设置android:windowSoftInputMode属性;
效果图:

Android输入法遮挡了输入框,使用android:fitsSystemWindows=

image

3、加入android:fitsSystemWindows="true"后,解决了输入法遮挡了输入框的问题,但是界面顶部出现了状态栏高度的白条。

解决方法

1、自定义CustomLinearLayout(因为我页面最外层是LinearLayout)继承LinearLayout,重写fitSystemWindows和onApplyWindowInsets两个方法:

public class CustomLinearLayout extends LinearLayout {
    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            insets.left = 0;
            insets.top = 0;
            insets.right = 0;
        }
        return super.fitSystemWindows(insets);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom()));
        } else {
            return insets;
        }
    }
} 

2、修改布局文件:

<com.example.widget.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_order_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"> 

3、配置文件不设置android:windowSoftInputMode属性;
4、效果图:

Android输入法遮挡了输入框,使用android:fitsSystemWindows=

image.png

问题解决。

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
android ContextMenu 上下文菜单示例
ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p
Wesley13 Wesley13
2年前
android Notification 状态栏通知使用示例
ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_
Wesley13 Wesley13
2年前
android WebView 使用实例
主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"
Wesley13 Wesley13
2年前
360项目
shape资源创建图片(重点)1.resdrawablexxx.xml<shapexmlns:android"http://schemas.android.com/apk/res/android"android:shape"rectangle"
Stella981 Stella981
2年前
Android toolbar 标题精确居中 不会因返回键偏移
1\.总的布局文件<android.support.design.widget.CoordinatorLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http://schemas.android.com
Stella981 Stella981
2年前
Android 复选框 以及回显
activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Stella981 Stella981
2年前
LISTVIew 加分割线
分割线样式文件:<?xmlversion"1.0"encoding"utf8"?<layerlistxmlns:android"http://schemas.android.com/apk/res/android"<item<
Stella981 Stella981
2年前
Android 控件抖动效果
利用Android自带的动画效果,实现控件的抖动效果,效果资源文件shark.xml<?xmlversion"1.0"encoding"utf8"?<translatexmlns:android"http://schemas.android.com/apk/res/android"android:fr
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"