fitSystemWindows到底能做什么? [英] What exactly does fitsSystemWindows do?

查看:132
本文介绍了fitSystemWindows到底能做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解fitsSystemWindows的概念,因为它依赖于它执行不同操作的视图.根据官方文档,这是一个

I'm struggling to understand the concept of fitsSystemWindows as depending on the view it does different things. According to the official documentation it's a

布尔型内部属性,用于根据系统窗口(例如状态栏)调整视图布局.如果为true,则将该视图的填充调整为为系统窗口留出空间.

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

Now, checking the View.java class I can see that when set to true, the window insets (status bar, navigation bar...) are applied to the view paddings, which works according to the documentation quoted above. This is the relevant part of the code:

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作为用于将视图放置在系统栏后面的标志.请参见此处.

With the new Material design there are new classes which make extensive use of this flag and this is where the confusion comes. In many sources fitsSystemWindows is mentioned as the flag to set to lay the view behind the system bars. See here.

ViewCompat.javasetFitsSystemWindows的文档说:

设置此视图是否应考虑系统屏幕装饰 例如状态栏并插入其内容; ,即控制是否 {@link View#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 View#fitSystemWindows(Rect)} will be executed. See that method for more details.

据此,fitsSystemWindows只是意味着将执行功能fitsSystemWindows()?新的Material类似乎只是在状态栏下使用它进行绘制.如果我们查看DrawerLayout.java的代码,我们可以看到以下内容:

According to this, fitsSystemWindows simply means that the function fitsSystemWindows() will be executed? The new Material classes seem to just use this for drawing under the status bar. If we look at DrawerLayout.java's code, we can see this:

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中看到了相同的模式.

And we see the same pattern in the new CoordinatorLayout or AppBarLayout.

这不是以与fitsSystemWindows文档完全相反的方式工作的吗?在最后一种情况下,这表示在系统栏后面绘制.

Doesn't this work in the exact opposite way as the documentation for fitsSystemWindows? In the last cases, it means draw behind the system bars.

但是,如果希望FrameLayout在状态栏后面绘制自身,则将fitsSystemWindows设置为true并不能解决问题,因为默认实现会执行最初记录的内容.您必须重写它,并添加与其他提到的类相同的标志.我想念什么吗?

However, if you want a FrameLayout to draw itself behind the status bar, setting fitsSystemWindows to true does not do the trick as the default implementation does what's documented initially. You have to override it and add the same flags as the other mentioned classes. Am I missing something?

推荐答案

系统窗口是系统绘制屏幕的部分 非交互式(对于状态栏)或交互式 (对于导航栏)内容.

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.

在大多数情况下,您的应用无需在状态栏或 导航栏,但如果要这样做:您需要确保交互式 元素(如按钮)并未隐藏在其下方.那就是 android:fitsSystemWindows ="true"属性的默认行为 给您:它设置视图的填充以确保内容 不要覆盖系统窗口.

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-开发人员/为什么我要适应systemwindows-4e26d9ce1eec

这篇关于fitSystemWindows到底能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆