什么是窗口集?

我正在尝试了解Android操作系统,当我阅读Google I / O 2014应用程序时,我遇到了.如果有人能解释他们是什么,那将是一个很大的帮助。谢谢。WindowInsets


答案 1

WindowInsets是应用于窗口的系统视图(例如状态栏、导航栏)的插图(或大小)。

在具体例子上很容易理解。图中所示:

enter image description here

现在,您不希望应用于 背景 ,因为在这种情况下,将按状态栏高度填充。WindowInsetsImageViewImageView

但是您确实希望将插图应用于 ,因为否则会在状态栏的中间位置绘制。ToolbarToolbar

该视图通过以下方式声明了在 xml 中应用的愿望:WindowInsets

android:fitsSystemWindows="true"

在此示例中,您无法将 应用于根布局,因为根布局将使用 ,并且 将填充 。WindowInsetsWindowInsetsImageView

相反,您可以使用将插图应用于工具栏:ViewCompat.setOnApplyWindowInsetsListener

ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, insets) -> {
            ((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin =
                    insets.getSystemWindowInsetTop();
            return insets.consumeSystemWindowInsets();
        });

请注意,当 的根布局传递给其子布局时,将调用此回调。布局如 、 不、 、 做 。ToolbarWindowsInsetsFrameLayoutLinearLayoutDrawerLayoutCoordinatorLayout

您可以对布局进行子类化,例如 并覆盖 :FrameLayoutonApplyWindowInsets

@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    int childCount = getChildCount();
    for (int index = 0; index < childCount; index++)
        getChildAt(index).dispatchApplyWindowInsets(insets); // let children know about WindowInsets

    return insets;
}

伊恩·莱克(Ian Lake)在Medium上有一篇关于这些东西的不错的博客文章,也是“成为一名窗户装配师傅”


答案 2

推荐