在一个沉浸式布局的Activity中存在多个设置了fitSystemWindows = "true"
的控件,但是却只有一个控件生效。例如tablayout+viewpager布局中,多个子页面存在设置了fitSystemWindows = "true"
的控件,但是只有第一个被找到的设置了fitSystemWindows = "true"
的控件生效。
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.
*
* 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);
}
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);
}
}
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;
}
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;
}
}
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);