2323import static java .lang .Math .min ;
2424
2525import android .content .Context ;
26- import android .content .res .Resources ;
2726import androidx .appcompat .widget .TintTypedArray ;
2827import android .util .AttributeSet ;
2928import 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
0 commit comments