gpt4 book ai didi

android - fitSystemWindows 到底是做什么的?

转载 作者:IT老高 更新时间:2023-10-28 13:02:21 27 4
gpt4 key购买 nike

我很难理解 fitsSystemWindows 的概念,因为它会根据 View 做不同的事情。根据官方文档,这是一个

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows.

现在,检查 View.java 类,我可以看到,当设置为 true 时,窗口插图(状态栏、导航栏...)应用于 View 填充,根据上面引用的文档工作。这是代码的相关部分:

private boolean fitSystemWindowsInt(Rect insets) {
if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
mUserPaddingStart = UNDEFINED_PADDING;
mUserPaddingEnd = UNDEFINED_PADDING;
Rect localInsets = sThreadLocal.get();
if (localInsets == null) {
localInsets = new Rect();
sThreadLocal.set(localInsets);
}
boolean res = computeFitSystemWindows(insets, localInsets);
mUserPaddingLeftInitial = localInsets.left;
mUserPaddingRightInitial = localInsets.right;
internalSetPadding(localInsets.left, localInsets.top,
localInsets.right, localInsets.bottom);
return res;
}
return false;
}

在新的 Material 设计中,新的类大量使用了这个标志,这就是困惑的地方。在许多来源中,fitsSystemWindows 被提及为设置为将 View 放置在系统栏后面的标志。见 here .

ViewCompat.javasetFitsSystemWindows 的文档说:

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 View#fitSystemWindows(Rect)} will be executed. See that method for more details.

据此,fitsSystemWindows只是表示函数fitsSystemWindows()会被执行?新的 Material 类似乎只是使用它在状态栏下进行绘图。如果我们查看 DrawerLayout.java 的代码,我们可以看到:

if (ViewCompat.getFitsSystemWindows(this)) {
IMPL.configureApplyInsets(this);
mStatusBarBackground = IMPL.getDefaultStatusBarBackground(context);
}

...

public static void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayoutImpl) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}

我们在新的 CoordinatorLayoutAppBarLayout 中看到了相同的模式。

这不是与 fitsSystemWindows 的文档完全相反吗?在最后一种情况下,这意味着在系统栏后面画

但是,如果您希望 FrameLayout 将自己绘制在状态栏后面,将 fitsSystemWindows 设置为 true 并不能解决问题,因为默认实现会执行最初记录的操作。您必须覆盖它并添加与其他提到的类相同的标志。我错过了什么吗?

最佳答案

System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content.

Most of the time, your app won’t need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren’t hidden underneath them. That’s what the default behavior of the android:fitsSystemWindows=“true” attribute gives you: it sets the padding of the View to ensure the contents don’t overlay the system windows.

https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec

关于android - fitSystemWindows 到底是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761046/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com