java 什么是 WindowInsets?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33585225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are WindowInsets?
提问by Samvid Mistry
I am trying to learn about the Android OS and while I was reading the Google I/O 2014 app, I came across the WindowInsets
. If anyone can explain what they are then it would be a great help. Thank you.
我正在尝试了解 Android 操作系统,在阅读 Google I/O 2014 应用程序时,我遇到了WindowInsets
. 如果有人可以解释它们是什么,那么这将是一个很大的帮助。谢谢你。
采纳答案by Henry
You can learn all about WindowInsets here.WindowInsets
provides you with the area on the window that is usable by the application. By itself it's of not much use. It's true purpose comes when you either override View.onApplyWindowInsets
or implement View.OnApplyWindowInsetsListener
. You can read about them here: View.onApplyWindowInsetsand View.OnApplyWindowInsetsListener
您可以在此处了解有关WindowInsets 的所有信息。WindowInsets
为您提供应用程序可用的窗口区域。它本身并没有多大用处。当您覆盖View.onApplyWindowInsets
或实施时,真正的目的就来了View.OnApplyWindowInsetsListener
。你可以在这里阅读它们:View.onApplyWindowInsets和View.OnApplyWindowInsetsListener
Listener for applying window insets on a view in a custom way.
Apps may choose to implement this interface if they want to apply custom policy to the way that window insets are treated for a view. If an OnApplyWindowInsetsListener is set, its onApplyWindowInsets method will be called instead of the View's own onApplyWindowInsets method. The listener may optionally call the parameter View's onApplyWindowInsets method to apply the View's normal behavior as part of its own.
以自定义方式在视图上应用窗口插图的侦听器。
如果应用程序想要将自定义策略应用于处理视图的窗口插图的方式,则它们可以选择实现此接口。如果设置了 OnApplyWindowInsetsListener,则将调用其 onApplyWindowInsets 方法而不是 View 自己的 onApplyWindowInsets 方法。侦听器可以选择调用参数 View 的 onApplyWindowInsets 方法来应用 View 的正常行为作为其自身的一部分。
In short, overriding this will let you control area of the window available for your View.
简而言之,覆盖它将使您可以控制可用于您的视图的窗口区域。
回答by azizbekian
WindowInsets
are insets (or sizes) of system views (e.g. status bar, navigation bar), that are applied to the window.
WindowInsets
是应用于窗口的系统视图(例如状态栏、导航栏)的插入(或大小)。
It would be easy to understand on concrete example. Image this scenario:
通过具体的例子很容易理解。想象一下这个场景:
Now, you don't want WindowInsets
to be applied to the background ImageView
, because in that case the ImageView
would be padded by status bar height.
现在,您不想WindowInsets
被应用到 background ImageView
,因为在这种情况下,ImageView
它将被状态栏高度填充。
But you do want insets to be applied to Toolbar
, because otherwise Toolbar
would be drawn somewhere mid status bar.
但是您确实希望将插图应用于Toolbar
,否则Toolbar
会被绘制在状态栏中间的某处。
The view declares a desire to apply WindowInsets
in xml by saying:
该视图通过以下语句声明了WindowInsets
在 xml 中应用的愿望:
android:fitsSystemWindows="true"
In this example you cannot apply the WindowInsets
to the root layout, because the root layout would consume WindowInsets
, and the ImageView
would be padded.
在此示例中,您不能将 应用于WindowInsets
根布局,因为根布局会消耗WindowInsets
,并且ImageView
将被填充。
Instead you may use ViewCompat.setOnApplyWindowInsetsListener
to apply insets to toolbar:
相反,您可以使用ViewCompat.setOnApplyWindowInsetsListener
将插图应用于工具栏:
ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, insets) -> {
((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();
return insets.consumeSystemWindowInsets();
});
Note, this callback would be called, when Toolbar
's root layout passes WindowsInsets
to its children. Layouts like FrameLayout
, LinearLayout
do not, DrawerLayout
, CoordinatorLayout
do.
请注意,当Toolbar
的根布局传递WindowsInsets
给其子项时,将调用此回调。布局,如FrameLayout
,LinearLayout
不DrawerLayout
,,CoordinatorLayout
做。
You can subclass your layout, e.g. FrameLayout
and override onApplyWindowInsets
:
您可以子类化您的布局,例如FrameLayout
并覆盖onApplyWindowInsets
:
@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;
}
There's a nice blog post at mediumby Ian Lake concerning this stuff, also "Becoming a master window fitter"presentation by Chris Banes.
Ian Lake在 medium上有一篇关于这些东西的不错的博客文章,还有Chris Banes 的“成为窗户装配大师”的演讲。
I've also created a detailed article at Mediumconcerning WindowInset
s.
我还在Medium 上创建了一篇关于WindowInset
s的详细文章。
More resources:
更多资源: