Loading

android只设置部分控件随着软键盘的出现而腾出空间

转载请标明出处:https://www.cnblogs.com/tangZH/p/12013685.html

更多精彩文章:http://77blogs.com/?p=276

 

在项目过程中,出现了一个需求,软键盘要顶起部分控件,而另一部分控件不动。

关于这种需求,我们需要明确布局方式:

1、线性布局是行不通的,即使被顶上去也是全部被顶上去,因为线性布局中里面的控件都是线性排列的,那么我们就用相对布局这种方式。

2、相对布局这种方式中,需要被顶上去的那一部分需要用一个父布局包裹起来,并且与不需要顶起来的那一部分不能有依赖关系,比如layout_above这类的,否则一个位置改变,另一个也会跟着改变。

项目中需要被顶起来的那一部分使用了:android:layout_alignParentBottom="true",置于底部。

 

其余的:

布局这样子之后,还要在manifests文件里面配置android:windowSoftInputMode="adjustResize"

 

然而会发现还是没有被顶起来,其实还差一个,在需要被顶起来的那一个父布局里面加上android:fitsSystemWindows="true"

 

这样又出现了另一个问题:当我们使用沉浸式状态栏的时候,设置android:fitsSystemWindows="true"会导致该父布局上面多出一块空白,据说这块空白的高度就是状态栏的高度。

 

最后发现可以用下面的方法解决:

需要被顶起来的一个父布局采用自定义的布局,然后重写相应的方法:

@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;
        }
    }

这样便解决了。

参考:https://blog.csdn.net/dbmonkey/article/details/84966318

 

posted @ 2019-12-09 21:53  妖久  阅读(1026)  评论(0编辑  收藏  举报