gpt4 book ai didi

android - fitsSystemWindows 到底是做什么的?

转载 作者:行者123 更新时间:2023-12-02 00:08:06 31 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 - fitsSystemWindows 到底是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58142870/

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