WindowInsets
are insets (or sizes) of system views (e.g. status bar, navigation bar), that are applied to the window.
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.
But you do want insets to be applied to Toolbar
, because otherwise Toolbar
would be drawn somewhere mid status bar.
The view declares a desire to apply WindowInsets
in xml by saying:
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.
Instead you may use ViewCompat.setOnApplyWindowInsetsListener
to apply insets to toolbar:
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.
You can subclass your layout, e.g. FrameLayout
and override 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 medium by Ian Lake concerning this stuff, also "Becoming a master window fitter??" presentation by Chris Banes.
I've also created a detailed article at Medium concerning WindowInset
s.
More resources: