Skip to content

Commit 801ccab

Browse files
imhappipekingme
authored andcommitted
[NavigationRail] Add new attributes for NavigationRail to control the top margin of the nav rail content, and the margin in between the optional header and the menu items
PiperOrigin-RevId: 637957360
1 parent e4f00fd commit 801ccab

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

docs/components/NavigationRail.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,22 @@ The following is an anatomy diagram for the navigation rail:
297297

298298
#### Header attributes
299299

300-
**Element** | **Attribute** | **Related methods** | **Default value**
301-
--------------- | ------------------ | ---------------------------------------------------------- | -----------------
302-
**Header view** | `app:headerLayout` | `addHeaderView`<br/>`removeHeaderView`<br/>`getHeaderView` | N/A
300+
**Element** | **Attribute** | **Related methods** | **Default value**
301+
------------------------ | ------------------------ | ---------------------------------------------------------- | -----------------
302+
**Header view** | `app:headerLayout` | `addHeaderView`<br/>`removeHeaderView`<br/>`getHeaderView` | N/A
303+
**Header bottom margin** | `app:headerMarginBottom` | N/A | `8dp`
303304

304305
See the
305306
[FAB documentation](https://github.com/material-components/material-components-android/tree/master/docs/components/FloatingActionButton.md)
306307
for more attributes.
307308

309+
#### Navigation Menu attributes
310+
311+
**Element** | **Attribute** | **Related methods** | **Default value**
312+
---------------- | ---------------------- | ------------------------------------- | -----------------
313+
**Menu gravity** | `app:menuGravity` | `setMenuGravity`<br/>`getMenuGravity` | `TOP\|CENTER_HORIZONTAL`
314+
**Top margin** | `app:contentMarginTop` | N/A | N/A
315+
308316
#### Navigation item attributes
309317

310318
**Element** | **Attribute** | **Related methods** | **Default value**

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static java.lang.Math.min;
2424

2525
import android.content.Context;
26-
import android.content.res.Resources;
2726
import androidx.appcompat.widget.TintTypedArray;
2827
import android.util.AttributeSet;
2928
import android.view.Gravity;
@@ -108,7 +107,8 @@ public class NavigationRailView extends NavigationBarView {
108107
private static final int DEFAULT_HEADER_GRAVITY = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
109108
static final int NO_ITEM_MINIMUM_HEIGHT = -1;
110109

111-
private final int topMargin;
110+
private final int contentMarginTop;
111+
private final int headerMarginBottom;
112112
@Nullable private View headerView;
113113
@Nullable private Boolean paddingTopSystemWindowInsets = null;
114114
@Nullable private Boolean paddingBottomSystemWindowInsets = null;
@@ -131,9 +131,6 @@ public NavigationRailView(
131131
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
132132
super(context, attrs, defStyleAttr, defStyleRes);
133133

134-
final Resources res = getResources();
135-
topMargin = res.getDimensionPixelSize(R.dimen.mtrl_navigation_rail_margin);
136-
137134
// Ensure we are using the correctly themed context rather than the context that was passed in.
138135
context = getContext();
139136

@@ -142,6 +139,13 @@ public NavigationRailView(
142139
ThemeEnforcement.obtainTintedStyledAttributes(
143140
context, attrs, R.styleable.NavigationRailView, defStyleAttr, defStyleRes);
144141

142+
contentMarginTop =
143+
attributes.getDimensionPixelSize(R.styleable.NavigationRailView_contentMarginTop,
144+
getResources().getDimensionPixelSize(R.dimen.mtrl_navigation_rail_margin));
145+
headerMarginBottom =
146+
attributes.getDimensionPixelSize(R.styleable.NavigationRailView_headerMarginBottom,
147+
getResources().getDimensionPixelSize(R.dimen.mtrl_navigation_rail_margin));
148+
145149
int headerLayoutRes = attributes.getResourceId(R.styleable.NavigationRailView_headerLayout, 0);
146150
if (headerLayoutRes != 0) {
147151
addHeaderView(headerLayoutRes);
@@ -236,7 +240,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
236240
super.onMeasure(minWidthSpec, heightMeasureSpec);
237241

238242
if (isHeaderViewVisible()) {
239-
int maxMenuHeight = getMeasuredHeight() - headerView.getMeasuredHeight() - topMargin;
243+
int maxMenuHeight = getMeasuredHeight() - headerView.getMeasuredHeight() - contentMarginTop
244+
- headerMarginBottom;
240245
int menuHeightSpec = MeasureSpec.makeMeasureSpec(maxMenuHeight, MeasureSpec.AT_MOST);
241246
measureChild(getNavigationRailMenuView(), minWidthSpec, menuHeightSpec);
242247
}
@@ -249,13 +254,13 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
249254
NavigationRailMenuView menuView = getNavigationRailMenuView();
250255
int offsetY = 0;
251256
if (isHeaderViewVisible()) {
252-
int usedTop = headerView.getBottom() + topMargin;
257+
int usedTop = headerView.getBottom() + headerMarginBottom;
253258
int menuTop = menuView.getTop();
254259
if (menuTop < usedTop) {
255260
offsetY = usedTop - menuTop;
256261
}
257262
} else if (menuView.isTopGravity()) {
258-
offsetY = topMargin;
263+
offsetY = contentMarginTop;
259264
}
260265

261266
if (offsetY > 0) {
@@ -296,7 +301,7 @@ public void addHeaderView(@NonNull View headerView) {
296301

297302
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
298303
params.gravity = DEFAULT_HEADER_GRAVITY;
299-
params.topMargin = topMargin;
304+
params.topMargin = contentMarginTop;
300305
addView(headerView, /* index= */ 0, params);
301306
}
302307

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<public name="itemMinHeight" type="attr"/>
2121
<public name="navigationRailStyle" type="attr"/>
2222
<public name="menuGravity" type="attr"/>
23+
<public name="contentMarginTop" type="attr"/>
24+
<public name="headerMarginBottom" type="attr"/>
2325
<public name="Widget.MaterialComponents.NavigationRailView" type="style"/>
2426
<public name="Widget.MaterialComponents.NavigationRailView.Compact" type="style"/>
2527
<public name="Widget.MaterialComponents.NavigationRailView.Colored" type="style"/>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<attr name="itemMinHeight" format="dimension"/>
2525
<!-- Specifies the layout that will be used to create the header view, if any -->
2626
<attr name="headerLayout"/>
27+
<!-- The bottom margin of the header -->
28+
<attr name="headerMarginBottom" format="dimension"/>
29+
<!-- The top margin of the content of the navigation rail. -->
30+
<attr name="contentMarginTop" format="dimension"/>
2731
<!-- Specifies how the navigation rail destinations should be aligned as a group. -->
2832
<attr name="menuGravity">
2933
<!-- Navigation rail destinations will be aligned as a group at the top. This is the default behavior. -->

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
<item name="labelVisibilityMode">auto</item>
4040
<item name="menuGravity">top</item>
4141
<item name="android:fitsSystemWindows">true</item>
42+
<item name="contentMarginTop">@dimen/mtrl_navigation_rail_margin</item>
43+
<item name="headerMarginBottom">@dimen/mtrl_navigation_rail_margin</item>
4244
</style>
4345

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

0 commit comments

Comments
 (0)