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-07-02 11:58:18

    探究Laravel使用env函数读取环境变量为null的问题

    最近在工作中遇到一个问题,不知道大家有没有遇到过,在 Laravel中(除 app/config 目录下的配置文件中)使用env函数读取环境变量,有时有用,有时返回 null,这究竟怎么回事?下面通过这篇文章让我们一探究竟。有需要的朋友们下面来一起看看吧。

  • 2018-07-10 16:56:00

    MUI-图片轮播控件

    图片轮播继承自slide插件,因此其DOM结构、事件均和slide插件相同; 在MUI框架中针对图片的轮播做了一个简单的封装。

  • 2018-07-10 16:56:42

    mysql in 排序 也可以按in里面的顺序来排序

    SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 这样的情况取出来后,其实,id还是按1,2,3,4,5,6,7,8,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能完成?是否需要取回来后再foreach一下?