Skip to content

Commit c66633b

Browse files
hunterstichpekingme
authored andcommitted
[NavigationRail] Added support for opting in/out of the NavigationRail automatically adding system top and bottom window insets.
PiperOrigin-RevId: 424424554
1 parent 0c8fc41 commit c66633b

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

catalog/java/io/material/catalog/navigationrail/res/layout/cat_navigation_rail_animated.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
android:id="@+id/cat_navigation_rail"
2121
android:layout_width="wrap_content"
2222
android:layout_height="match_parent"
23-
app:menu="@menu/navigation_rail_animated_menu"/>
23+
app:menu="@menu/navigation_rail_animated_menu"
24+
android:fitsSystemWindows="false"/>
2425

catalog/java/io/material/catalog/navigationrail/res/layout/cat_navigation_rail_fragment.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
-->
1717
<LinearLayout
1818
xmlns:android="http://schemas.android.com/apk/res/android"
19-
xmlns:app="http://schemas.android.com/apk/res-auto"
20-
android:id="@+id/content"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/content"
2121
android:baselineAligned="false"
2222
android:layout_width="match_parent"
2323
android:layout_height="match_parent"
@@ -26,7 +26,8 @@
2626
android:id="@+id/cat_navigation_rail"
2727
android:layout_width="wrap_content"
2828
android:layout_height="match_parent"
29-
app:menu="@menu/navigation_rail_menu"/>
29+
app:menu="@menu/navigation_rail_menu"
30+
android:fitsSystemWindows="false"/>
3031

3132
<ScrollView
3233
android:layout_width="0dp"

lib/java/com/google/android/material/navigationrail/NavigationRailView.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public class NavigationRailView extends NavigationBarView {
103103

104104
private final int topMargin;
105105
@Nullable private View headerView;
106+
@Nullable private Boolean paddingTopSystemWindowInsets = null;
107+
@Nullable private Boolean paddingBottomSystemWindowInsets = null;
106108

107109
public NavigationRailView(@NonNull Context context) {
108110
this(context, null);
@@ -146,6 +148,17 @@ public NavigationRailView(
146148
R.styleable.NavigationRailView_itemMinHeight, NO_ITEM_MINIMUM_HEIGHT));
147149
}
148150

151+
if (attributes.hasValue(R.styleable.NavigationRailView_paddingTopSystemWindowInsets)) {
152+
paddingTopSystemWindowInsets =
153+
attributes.getBoolean(
154+
R.styleable.NavigationRailView_paddingTopSystemWindowInsets, false);
155+
}
156+
if (attributes.hasValue(R.styleable.NavigationRailView_paddingBottomSystemWindowInsets)) {
157+
paddingBottomSystemWindowInsets =
158+
attributes.getBoolean(
159+
R.styleable.NavigationRailView_paddingBottomSystemWindowInsets, false);
160+
}
161+
149162
attributes.recycle();
150163

151164
applyWindowInsets();
@@ -163,8 +176,12 @@ public WindowInsetsCompat onApplyWindowInsets(
163176
@NonNull RelativePadding initialPadding) {
164177
// Apply the top, bottom, and start padding for a start edge aligned
165178
// NavigationRailView to dodge the system status and navigation bars
166-
initialPadding.top += insets.getSystemWindowInsetTop();
167-
initialPadding.bottom += insets.getSystemWindowInsetBottom();
179+
if (shouldApplyWindowInsetPadding(paddingTopSystemWindowInsets)) {
180+
initialPadding.top += insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
181+
}
182+
if (shouldApplyWindowInsetPadding(paddingBottomSystemWindowInsets)) {
183+
initialPadding.top += insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
184+
}
168185

169186
boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
170187
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
@@ -176,6 +193,16 @@ public WindowInsetsCompat onApplyWindowInsets(
176193
});
177194
}
178195

196+
/**
197+
* Whether the top or bottom of this view should be padded in to avoid the system window insets.
198+
*
199+
* If the {@code paddingInsetFlag} is set, that value will take precedent. Otherwise,
200+
* fitsSystemWindow will be used.
201+
*/
202+
private boolean shouldApplyWindowInsetPadding(Boolean paddingInsetFlag) {
203+
return paddingInsetFlag != null ? paddingInsetFlag : ViewCompat.getFitsSystemWindows(this);
204+
}
205+
179206
@Override
180207
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
181208
int minWidthSpec = makeMinWidthSpec(widthMeasureSpec);

lib/java/com/google/android/material/navigationrail/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<!-- Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL -->
3737
<enum name="bottom" value="81"/>
3838
</attr>
39+
<!-- Whether the navigation rail should apply padding to have its menu
40+
items and optional header below the top window insets. -->
41+
<attr name="paddingTopSystemWindowInsets"/>
42+
<!-- Whether the navigation rail should apply padding to have its menu
43+
items above the bottom window insets. -->
44+
<attr name="paddingBottomSystemWindowInsets"/>
3945
</declare-styleable>
4046

4147
</resources>

lib/java/com/google/android/material/navigationrail/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<item name="itemTextColor">@color/mtrl_navigation_bar_item_tint</item>
3939
<item name="labelVisibilityMode">auto</item>
4040
<item name="menuGravity">top</item>
41+
<item name="android:fitsSystemWindows">true</item>
4142
</style>
4243

4344
<style name="Widget.MaterialComponents.NavigationRailView.Compact">

0 commit comments

Comments
 (0)