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-09-26 15:14:23

    PHP JSON_ENCODE 不转义中文汉字的方法

    PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据。 网上很多,但是其实都是错误的,正确的方法是在json_encode 中加入一个参数 JSON_UNESCAPED_UNICODE

  • 2018-09-27 10:04:11

    jquery ajax超时设置

    原来ajax可以设置超时时间,那么简单,ajax还有更多功能,虽然不怎么用它,有时候还挺好用。

  • 2018-10-10 14:29:01

    php header()函数设置页面Cache缓存

    ​ header()函数在php的使用很大,下面我来介绍利用它实现页面缓存的一些方法,但使用header前必须注意,在它之前不能任何输出,包括空格。