android adjustresize not working when using windowTranslucentStatus

做项目的时候,用沉浸式状态栏,最终实现的效果如下:

《android adjustresize not working when using windowTranslucentStatus》 304079-6b9882b14cb93284.png

上图中是一个聊天界面,当输入法弹出的时候需要把Editteext顶起,本来把Edittext顶起这是一件多么简单的事,只需要在manifest清单文件设置<android:windowSoftInputMode=”adjustResize”>就可以了,然而当我适配完沉浸式状态栏后,兴致勃勃地运行项目一看,妈的傻眼了,竟然出现了下图这种情况:

《android adjustresize not working when using windowTranslucentStatus》 304079-3734a115e712a6df.png

是的,你没看错,ToolBar竟然往下跑了,Edittext也没有被弹起。我当时还怀疑没运行成功,又运行了几遍,没想到还是一样的结果,又检查了几遍代码,确定这样写没错后,就立马google了起来,不搜不知道,一搜吓一跳,网上一大堆这种问题,看来不少人都踩了这坑,然后找到了解决方法:windowsoftinputmode-adjustresize-not-working-with-translucen

下面这个是在webView的界面显示:

《android adjustresize not working when using windowTranslucentStatus》 304079-2a9cf5db6c9d643c.png

解决后的界面:

《android adjustresize not working when using windowTranslucentStatus》 304079-420c6663373db717.png

解决方法就是从写了fitSystemWindows方法,然后将重写的布局作为根布局,再设置fitSystemWindows = true:

  • LinearLayout:
/**
 * 解决activity设置了状态栏透明,又设置了android:windowSoftInputMode="adjustResize"的时候输入框被输入法挡住的情况<br>
 *     这个作为layout的根布局(root),并设置<code>android:fitsSystemWindows="true"</code>
 * Created by Awen <Awentljs@gmail.com>
 */
public class CustomInsetsLinearLayout extends LinearLayout {
    private int[] mInsets = new int[4];

    public CustomInsetsLinearLayout(Context context) {
        super(context);
    }

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

    public CustomInsetsLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public final int[] getInsets() {
        return mInsets;
    }

    @Override
    protected final boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Intentionally do not modify the bottom inset. For some reason,
            // if the bottom inset is modified, window resizing stops working.
            // TODO: Figure out why.

            mInsets[0] = insets.left;
            mInsets[1] = insets.top;
            mInsets[2] = insets.right;

            insets.left = 0;
            insets.top = 0;
            insets.right = 0;
        }

        return super.fitSystemWindows(insets);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<com.demo.widget.insets.CustomInsetsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <-- main content,这里是你的主要内容-->
    
    
</com.demo.widget.insets.CustomInsetsLinearLayout>


如果需要用到RelativeLayout或FrameLayout的,把上面的继承父类改为RelativeLayout或FrameLayout就可以了。

另外,贴上一个Android键盘面板冲突 布局闪动处理方案,也就是输入法跟表情切换顺畅,跟微信一样,这个解决方法也是微信团队的人出品的,非常感谢,在这里我也要分享下给大家,因为我发现还有很多app都没解决这个问题

最后祝大家周末愉快。

    原文作者:八进制
    原文地址: https://www.jianshu.com/p/4f744c95a51c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞