Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG

2018-06-02 00:30:35

一.前言

在开发中,如果输入框在布局的底部。在弹出输入发时,为了使输入法不遮挡输入框通常有两种做法: 
1.将布局压缩(Activity的android:windowSoftInputMode属性设置为”adjustResize”)。 
2.移动布局,将布局顶到输入框之上(Activity的android:windowSoftInputMode属性设置为”adjustPan”)

在使用沉浸式状态栏之后,发现将布局压缩的方法没用了(Activity的android:windowSoftInputMode属性设置为”adjustResize”了),但是移动布局的方式还是有用的。

二.解决方法

不知道这是不是Android的一个BUG,找了很多资料,才发现有以下一种解决方法。

1.自定义ViewGroup(LinearLayout,RelativeLayout等),重写fitSystemWindows方法,如下:

public class MyLinearLayout extends LinearLayout {

    public MyLinearLayout(Context context) {        super(context);
    }    public MyLinearLayout(Context context, AttributeSet attrs) {        super(context, attrs);
    }    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);
    }    @Override
    protected boolean fitSystemWindows(Rect insets) {
        insets.top = 0;        return super.fitSystemWindows(insets);
    }
}1234567891011121314151617181920

2.将原先的xml布局的根ViewGroup换成我们自定义的ViewGroup。

3.在Activity或Fragment中加载出该自定义ViewGroup,然后调用setFitsSystemWindows(true)方法,如下:

MyLinearLayout linearLayout = (MyLinearLayout) findViewById(R.id.activityMain_mRootView);linearLayout.setFitsSystemWindows(true);12

最好在Activity或Fragment销毁时调用linearLayout.setFitsSystemWindows(false);

至此完毕,如有错误,欢迎指教。


  • 2018-03-08 09:44:12

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:44:15

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:47:14

    ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组。

  • 2018-03-08 09:53:39

    document.readyState

    一个document 的 Document.readyState 属性描述了文档的加载状态。

  • 2018-03-09 02:09:23

    ArrayBuffer:类型化数组

    ArrayBuffer对象、TypedArray对象、DataView对象是JavaScript操作二进制数据的一个接口。这些对象早就存在,属于独立的规格,ES6将它们纳入了ECMAScript规格,并且增加了新的方法。

  • 2018-03-09 11:45:11

    SQL SELECT DISTINCT 语句

    如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句: