fitsSystemWindows=true 失效问题

本文深入探讨了在沉浸式布局的Activity中使用fitSystemWindows属性时遇到的问题,即仅首个控件生效的现象。通过源码分析揭示了原因在于父类ViewGroup在分发WindowInsets时的处理机制,并提供了两种解决方案:强制分发WindowInsets和手动设置padding。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fitSystemWindows = "true"失效问题

问题场景:

在一个沉浸式布局的Activity中存在多个设置了fitSystemWindows = "true"的控件,但是却只有一个控件生效。例如tablayout+viewpager布局中,多个子页面存在设置了fitSystemWindows = "true"的控件,但是只有第一个被找到的设置了fitSystemWindows = "true"的控件生效。

问题排查:
  1. 查看View中设置fitSystemWindows = "true"的源码,通过注释得知是通过fitSystemWindows(Rect)方法产生效果。
  /**
     * Sets whether or not this view should account for system screen decorations
     * such as the status bar and inset its content; that is, controlling whether
     * the default implementation of {@link #fitSystemWindows(Rect)} will be
     * executed.  See that method for more details.
     *
     * <p>Note that if you are providing your own implementation of
     * {@link #fitSystemWindows(Rect)}, then there is no need to set this
     * flag to true -- your implementation will be overriding the default
     * implementation that checks this flag.
     *
     * @param fitSystemWindows If true, then the default implementation of
     * {@link #fitSystemWindows(Rect)} will be executed.
     *
     * @attr ref android.R.styleable#View_fitsSystemWindows
     * @see #getFitsSystemWindows()
     * @see #fitSystemWindows(Rect)
     * @see #setSystemUiVisibility(int)
     */
    public void setFitsSystemWindows(boolean fitSystemWindows) {
        setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
    }
  1. 继续查看fitSystemWindows(Rect insets)方法,得知:
    • 处理任务被转到dispatchApplyWindowInsets(WindowInsets insets)方法中进行分发。
    • 这个方法有参数传递,通过查找被调用发现onApplyWindowInsets(WindowInsets insets)方法。
	@Deprecated
    protected boolean fitSystemWindows(Rect insets) {
        if ((mPrivateFlags3 & PFLAG3_APPLYING_INSETS) == 0) {
            if (insets == null) {
                // Null insets by definition have already been consumed.
                // This call cannot apply insets since there are none to apply,
                // so return false.
                return false;
            }
            // If we're not in the process of dispatching the newer apply insets call,
            // that means we're not in the compatibility path. Dispatch into the newer
            // apply insets path and take things from there.
            try {
                mPrivateFlags3 |= PFLAG3_FITTING_SYSTEM_WINDOWS;
                return dispatchApplyWindowInsets(new WindowInsets(insets)).isConsumed();
            } finally {
                mPrivateFlags3 &= ~PFLAG3_FITTING_SYSTEM_WINDOWS;
            }
        } else {
            // We're being called from the newer apply insets path.
            // Perform the standard fallback behavior.
            return fitSystemWindowsInt(insets);
        }
    }
  1. 继续查看onApplyWindowInsets(WindowInsets insets)方法,发现这个方式只是消耗了WindowInsets。查找方法调用发现dispatchApplyWindowInsets(WindowInsets insets)方法
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if ((mPrivateFlags3 & PFLAG3_FITTING_SYSTEM_WINDOWS) == 0) {
            // We weren't called from within a direct call to fitSystemWindows,
            // call into it as a fallback in case we're in a class that overrides it
            // and has logic to perform.
            if (fitSystemWindows(insets.getSystemWindowInsets())) {
                return insets.consumeSystemWindowInsets();
            }
        } else {
            // We were called from within a direct call to fitSystemWindows.
            if (fitSystemWindowsInt(insets.getSystemWindowInsets())) {
                return insets.consumeSystemWindowInsets();
            }
        }
        return insets;
    }
  1. 查看dispatchApplyWindowInsets(WindowInsets insets)方法,得知这是个分发方法,只是将WindowInsets消耗和传递到下一级处理回调中。由问题场景可知,问题不是在下一级处理中,而是应该来自父类事件分发产生的,通过查找调用发现事件来自ViewGroup的dispatchApplyWindowInsets(WindowInsets insets)方法
public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
        try {
            mPrivateFlags3 |= PFLAG3_APPLYING_INSETS;
            if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) {
                return mListenerInfo.mOnApplyWindowInsetsListener.onApplyWindowInsets(this, insets);
            } else {
                return onApplyWindowInsets(insets);
            }
        } finally {
            mPrivateFlags3 &= ~PFLAG3_APPLYING_INSETS;
        }
    }
  1. 查看ViewGroup中dispatchApplyWindowInsets(WindowInsets insets)方法,发现这里分发WindowInsets时查到第一个消耗WindowInsets的子控件就返回了,WindowInsets不再继续传递给其他子控件
	@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;
    }
排查结果:

到此源码查看完毕,设置了fitSystemWindows = "true"会消耗WindowInsets事件,而 父类ViewGroup传递WindowInsets事件只给第一个消耗事件的子控件,因此导致了上述问题。

解决方案

方案一、 既然父类没有给分发WindowInsets事件,那就手动强行分发:
1. 重写父类分发;
2. 手动调用分发方法,传值当前窗口的WindowInsets(具体方案如下)
先给控件设置setFitsSystemWindows(true)/android:fitsSystemWindows=“true”

View decorView = window.getDecorView();
WindowInsets windowInsets = decorView.getRootWindowInsets();
if (windowInsets != null) {
    goalView.dispatchApplyWindowInsets(windowInsets.replaceSystemWindowInsets(0, windowInsets.getSystemWindowInsetTop(), 0, 0));
    goalView.setOnApplyWindowInsetsListener((v, insets) -> insets);
}

方案二、无需setFitsSystemWindows(true)/android:fitsSystemWindows=“true”,强行手动设置padding

View decorView = window.getDecorView();
WindowInsets windowInsets = decorView.getRootWindowInsets();
goalView.setPadding(0,windowInsets.getSystemWindowInsetTop(),0,0);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值