美文网首页Android开发Android 知识1-Android开发知识
一个 fitSystemWindows 失效问题的解决实例

一个 fitSystemWindows 失效问题的解决实例

作者: summerlyy | 来源:发表于2018-05-31 11:58 被阅读72次

    ​ 在写Android音乐播放器 Quiet 的时候,遇到一个奇怪的BUG, 布局的 fitSystemWindows属性某些场景下会不起作用。

    业务场景

    ​ 具体场景是这样的。首先有一个界面 FmPlayerFragment用于控制 FM的播放,它的具体实现后的界面长下图这个样子:

    FmPlayerFragment

    ​ 可以看出背景图是显示在整个屏幕中的,包括状态栏底部导航。为了实现这个效果,具体的的XML代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/imageBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:foreground="@drawable/player_gradient_fm_mask"
            android:scaleType="centerCrop"
            app:srcCompat="?colorPrimary" />
    
        <LinearLayout
            android:id="@+id/fmPlayerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:orientation="vertical">
    
            <!-- 省略其他的布局信息 -->
    
        </LinearLayout>
    
    
    </android.support.constraint.ConstraintLayout>
    

    ​ 也就是为 fmPlayerLayout 这个布局加入一个 fitsSystemWindows = true 这一个属性。然后调用的时候使用如下语句即可。

    supportFragmentManager.intransaction {
        replace(android.R.id.content, fragment, fragmentName)
        addToBackStack(fragmentName)
    }
    

    ​ 当然这还不够,还需要使用 view?.requestApplyInsets() 来请求 RootViewImpl 来分发 WindowInsets 信息以让 fitSystemWindows 属性生效。注意:requestApplyInsets方法需要在这个view已经attach到root view 上,也就是依附在当前屏幕中的布局才会有效,所以请在fragment的onStart回调中调用此方法。

    出BUG了

    ​ 前一个场景看起来实现逻辑并没有问题——使用 fitSystemWindow 来让屏幕内容适配正常的显示区域,并通过requestApplyInsets()方法来让该属性生效,在大部分Activity上的显示也很正常。

    但是,在某些Activity上却出现了下面这样的情况!!(也就是说 fitSystemWindows属性并未生效。

    错位的布局

    ​ 出BUG需要寻找原因,但是在此之前需要了解一下 fitSystemWindows属性生效的原理。

    FitSystemWidows原理

    fitSystemWindows 以及 WindowInsets的分发机制,网上已经有了很多的文章了,所以并不作详细说明,就简单介绍介绍好了。

    WindowInsets就是Android视图中用于调节窗口信息显示的实体。主要介绍下面几个方法

    /** WindowInsets.java**/
    public int getSystemWindowInsetLeft();
    public int getSystemWindowInsetTop();
    public int getSystemWindowInsetRight();
    public int getSystemWindowInsetBottom();
    

    ​ 分别为获取系统窗口的上下左右偏移数据。比如上面例子,在 ViewRootImpl中分发下来的 WindowInsets的 top 和 bottom 就分别为 63 和 126 (这两个数值应该没记错吧),分别就是状态栏和导航栏的高度。

    ​ 分发主要通过View#dispatchApplyWindowInsets方法来完成,而 ViewGroup 中的分发逻辑是这样的:

    @Override
    public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
        insets = super.dispatchApplyWindowInsets(insets);
        if (!insets.isConsumed()) {
            final int count = getChildCount();
            for (int i = 0; i < count; i++) {
                insets = getChildAt(i).dispatchApplyWindowInsets(insets);
                if (insets.isConsumed()) {
                    break;
                }
            }
        }
        return insets;
    }
    

    ​ 逻辑很简单,一眼可以看出:只要 insets 被消耗掉了,就终止分发的过程。

    ​ View中的dispatchApplyWindowInsets代码就不贴了,就是判断是否fitSystemWindows属性为true,如果为true,那么将调用 mListenerInfo.mOnApplyWindowInsetsListener 或者为当前 View 设置相应的padding。

    寻找BUG的原因

    ​ 那么导致 FmPlayerFragment 布局出错的原因肯定是 WindowInsets 在分发的某一个步骤中被消耗掉了。

    ​ 消耗 WindowInsets 是通过 WindowInsets.consumeSystemWindowInsets方法来完成的,所以 debug 时为这个方法下个断点就行了。

    WindowInsets.java

    ​ 具体看下去,emmm,原来是 ScrimInsetsFrameLayout 这小子。

    ScrimInsetsFrameLayout.java

    ​ ScrimInsetsFrameLayout 就一个子类 NavigationView。可以算是破案了。因为我在MainActivity中用到了NavigationView来做侧边导航。

    解决问题

    ​ ScrimInsetsFrameLayout在构造方法中就注册了监听WindowInsets的方法,回调中就把它给消耗掉了,在这个过程中却没有检查View的fitSystemWindows属性是否为真。

    所以已经向Google提交了BUG,希望下个版本能在这加入一个判断。

    临时解决办法 需要两个步骤

    1. 将NavigationView的fitSystemWindows属性显式的设置为false

    2. 调用navigationView.setOnApplyWindowInsetsListener(null)清除该View对 WindowInsets分发的监听。

    ​ 当然,这样的临时解决方案会有一些副作用,会使NavigationView无法监听到WindowInsets的分发,这样的话NavigationView导航项过多的话,可能会导致NavigationView中的Menu显示到底部导航栏中而点击不到,所以慎用!!!

    总结

    ​ 了解WindowInsets的分发应该还是有用的吧,毕竟此方案还可以解决异形屏幕的显示问题,也就是最近流行的什么刘海,美人尖什么的...

    相关文章

      网友评论

        本文标题:一个 fitSystemWindows 失效问题的解决实例

        本文链接:https://www.haomeiwen.com/subject/uekqsftx.html