@@ -173,6 +173,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
173173 @ TitleCollapseMode private int titleCollapseMode ;
174174
175175 @ Nullable WindowInsetsCompat lastInsets ;
176+ private int topInsetApplied = 0 ;
177+ private boolean forceApplySystemWindowInsetTop = false ;
176178
177179 public CollapsingToolbarLayout (@ NonNull Context context ) {
178180 this (context , null );
@@ -273,6 +275,9 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
273275
274276 toolbarId = a .getResourceId (R .styleable .CollapsingToolbarLayout_toolbarId , -1 );
275277
278+ forceApplySystemWindowInsetTop =
279+ a .getBoolean (R .styleable .CollapsingToolbarLayout_forceApplySystemWindowInsetTop , false );
280+
276281 a .recycle ();
277282
278283 setWillNotDraw (false );
@@ -516,9 +521,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
516521
517522 final int mode = MeasureSpec .getMode (heightMeasureSpec );
518523 final int topInset = lastInsets != null ? lastInsets .getSystemWindowInsetTop () : 0 ;
519- if (mode == MeasureSpec .UNSPECIFIED && topInset > 0 ) {
520- // If we have a top inset and we're set to wrap_content height we need to make sure
521- // we add the top inset to our height, therefore we re-measure
524+ if ((mode == MeasureSpec .UNSPECIFIED || forceApplySystemWindowInsetTop ) && topInset > 0 ) {
525+ // If we have a top inset and we're set to wrap_content height or force apply,
526+ // we need to make sure we add the top inset to our height, therefore we re-measure
527+ topInsetApplied = topInset ;
522528 heightMeasureSpec =
523529 MeasureSpec .makeMeasureSpec (getMeasuredHeight () + topInset , MeasureSpec .EXACTLY );
524530 super .onMeasure (widthMeasureSpec , heightMeasureSpec );
@@ -1310,6 +1316,24 @@ public boolean isRtlTextDirectionHeuristicsEnabled() {
13101316 return collapsingTextHelper .isRtlTextDirectionHeuristicsEnabled ();
13111317 }
13121318
1319+ /**
1320+ * Sets whether the top system window inset should be respected regardless of what the
1321+ * {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
1322+ */
1323+ @ RestrictTo (LIBRARY_GROUP )
1324+ public void setForceApplySystemWindowInsetTop (boolean forceApplySystemWindowInsetTop ) {
1325+ this .forceApplySystemWindowInsetTop = forceApplySystemWindowInsetTop ;
1326+ }
1327+
1328+ /**
1329+ * Gets whether the top system window inset should be respected regardless of what the
1330+ * {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
1331+ */
1332+ @ RestrictTo (LIBRARY_GROUP )
1333+ public boolean isForceApplySystemWindowInsetTop () {
1334+ return forceApplySystemWindowInsetTop ;
1335+ }
1336+
13131337 /**
13141338 * Set the amount of visible height in pixels used to define when to trigger a scrim visibility
13151339 * change.
@@ -1338,7 +1362,7 @@ public void setScrimVisibleHeightTrigger(@IntRange(from = 0) final int height) {
13381362 public int getScrimVisibleHeightTrigger () {
13391363 if (scrimVisibleHeightTrigger >= 0 ) {
13401364 // If we have one explicitly set, return it
1341- return scrimVisibleHeightTrigger ;
1365+ return scrimVisibleHeightTrigger + topInsetApplied ;
13421366 }
13431367
13441368 // Otherwise we'll use the default computed value
0 commit comments