Skip to content

Commit b34fff3

Browse files
imhappidrchen
authored andcommitted
[NavigationBar] Add item gravity attribute and setter/getter
PiperOrigin-RevId: 660062964
1 parent 9c4ba8f commit b34fff3

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

docs/components/BottomNavigation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ The following is an anatomy diagram for the bottom navigation bar:
276276
**Ripple (inactive)** | `app:itemRippleColor` | `setItemRippleColor`<br/>`getItemRippleColor` | Variations of `?attr/colorPrimary` and `?attr/colorOnSurfaceVariant` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/bottomnavigation/res/color/m3_navigation_bar_ripple_color_selector.xml))
277277
**Ripple (active)** | " | " | Variations of `?attr/colorPrimary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/bottomnavigation/res/color/m3_navigation_bar_ripple_color_selector.xml))
278278
**Label visibility mode** | `app:labelVisibilityMode` | `setLabelVisibilityMode`<br/>`getLabelVisibilityMode` | `LABEL_VISIBILITY_AUTO`
279-
279+
**Item Gravity** | `app:itemGravity` | `setItemGravity`<br/>`getItemGravity` | `TOP_CENTER`
280280

281281
#### Active indicator attributes
282282

docs/components/NavigationRail.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ for more attributes.
323323
**Label visibility mode** | `app:labelVisibilityMode` | `setLabelVisibilityMode`<br/>`getLabelVisibilityMode` | `LABEL_VISIBILITY_AUTO`
324324
**Item minimum height** | `app:itemMinHeight` | `setItemMinimumHeight`<br/>`getItemMinimumHeight` | `NO_ITEM_MINIMUM_HEIGHT`
325325
**Item spacing** | `app:itemSpacing` | `setItemSpacing`<br/>`getItemSpacing` | `0dp`
326+
**Item Gravity** | `app:itemGravity` | `setItemGravity`<br/>`getItemGravity` | `TOP_CENTER`
326327

327328
**Note:** If there's not enough room, `itemMinHeight` and `itemSpacing` may not be respected in order to fit the items.
328329

lib/java/com/google/android/material/navigation/NavigationBarItemView.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.google.android.material.badge.BadgeUtils;
7575
import com.google.android.material.internal.BaselineLayout;
7676
import com.google.android.material.motion.MotionUtils;
77+
import com.google.android.material.navigation.NavigationBarView.ItemGravity;
7778
import com.google.android.material.navigation.NavigationBarView.ItemIconGravity;
7879
import com.google.android.material.resources.MaterialResources;
7980
import com.google.android.material.ripple.RippleUtils;
@@ -147,6 +148,7 @@ public abstract class NavigationBarItemView extends FrameLayout implements MenuV
147148

148149
@ItemIconGravity private int itemIconGravity;
149150
private int badgeFixedEdge = BadgeDrawable.BADGE_FIXED_EDGE_START;
151+
@ItemGravity private int itemGravity = NavigationBarView.ITEM_GRAVITY_TOP_CENTER;
150152

151153
public NavigationBarItemView(@NonNull Context context) {
152154
super(context);
@@ -292,16 +294,12 @@ public void setLabelVisibilityMode(@NavigationBarView.LabelVisibility int mode)
292294
}
293295

294296
private void updateItemIconGravity() {
295-
int gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
296297
int sideMargin = 0;
297298
int labelGroupTopMargin = activeIndicatorLabelPadding;
298299
int labelGroupSideMargin = 0;
299300
int sidePadding = 0;
300-
int contentGravity = Gravity.CENTER;
301301
badgeFixedEdge = BadgeDrawable.BADGE_FIXED_EDGE_START;
302302
if (itemIconGravity == ITEM_ICON_GRAVITY_START) {
303-
gravity = Gravity.CENTER;
304-
contentGravity = Gravity.START | Gravity.CENTER_VERTICAL;
305303
sideMargin =
306304
getResources()
307305
.getDimensionPixelSize(R.dimen.m3_expressive_navigation_item_leading_trailing_space);
@@ -318,12 +316,11 @@ private void updateItemIconGravity() {
318316
contentContainer.addView(labelGroup);
319317
}
320318
FrameLayout.LayoutParams contentContainerLp = (LayoutParams) contentContainer.getLayoutParams();
321-
contentContainerLp.gravity = gravity;
319+
contentContainerLp.gravity = itemGravity;
322320
FrameLayout.LayoutParams innerContentLp =
323321
(LayoutParams) innerContentContainer.getLayoutParams();
324322
innerContentLp.leftMargin = sideMargin;
325323
innerContentLp.rightMargin = sideMargin;
326-
innerContentLp.gravity = contentGravity;
327324
LinearLayout.LayoutParams labelGroupLp =
328325
(LinearLayout.LayoutParams) labelGroup.getLayoutParams();
329326
labelGroupLp.rightMargin =
@@ -466,9 +463,7 @@ private void setLayoutConfigurationIconAndLabel(
466463
contentContainer,
467464
itemIconGravity == ITEM_ICON_GRAVITY_TOP ? (int) (itemPaddingTop + topMarginShift) : 0,
468465
0,
469-
itemIconGravity == ITEM_ICON_GRAVITY_TOP
470-
? Gravity.CENTER_HORIZONTAL | Gravity.TOP
471-
: Gravity.CENTER);
466+
itemGravity);
472467
setViewMarginAndGravity(
473468
innerContentContainer,
474469
0,
@@ -484,7 +479,8 @@ private void setLayoutConfigurationIconAndLabel(
484479
}
485480

486481
private void setLayoutConfigurationIconOnly() {
487-
setViewMarginAndGravity(contentContainer, itemPaddingTop, itemPaddingTop, Gravity.CENTER);
482+
setViewMarginAndGravity(contentContainer, itemPaddingTop, itemPaddingTop,
483+
itemIconGravity == ITEM_ICON_GRAVITY_TOP ? Gravity.CENTER : itemGravity);
488484
setViewMarginAndGravity(innerContentContainer, 0, 0, Gravity.CENTER);
489485
updateViewPaddingBottom(labelGroup, 0);
490486
labelGroup.setVisibility(GONE);
@@ -870,6 +866,14 @@ public void setActiveIndicatorEnabled(boolean enabled) {
870866
requestLayout();
871867
}
872868

869+
/**
870+
* Set the gravity of the item.
871+
*/
872+
public void setItemGravity(@ItemGravity int itemGravity) {
873+
this.itemGravity = itemGravity;
874+
requestLayout();
875+
}
876+
873877
/**
874878
* Set the desired width of the active indicator.
875879
*

lib/java/com/google/android/material/navigation/NavigationBarMenuView.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public abstract class NavigationBarMenuView extends ViewGroup implements MenuVie
109109

110110
private int itemActiveIndicatorMarginHorizontal;
111111
private int itemActiveIndicatorExpandedMarginHorizontal;
112+
private int itemGravity = NavigationBarView.ITEM_GRAVITY_TOP_CENTER;
112113
private ShapeAppearanceModel itemActiveIndicatorShapeAppearance;
113114
private boolean itemActiveIndicatorResizeable = false;
114115
private ColorStateList itemActiveIndicatorColor;
@@ -475,6 +476,30 @@ public void setItemActiveIndicatorHeight(@Px int height) {
475476
}
476477
}
477478

479+
/**
480+
* Sets the navigation items' layout gravity.
481+
*
482+
* @param itemGravity the layout {@link android.view.Gravity} of the item
483+
* @see #getItemGravity()
484+
*/
485+
public void setItemGravity(int itemGravity) {
486+
this.itemGravity = itemGravity;
487+
if (buttons != null) {
488+
for (NavigationBarItemView item : buttons) {
489+
item.setItemGravity(itemGravity);
490+
}
491+
}
492+
}
493+
494+
/**
495+
* Returns the navigation items' layout gravity.
496+
*
497+
* @see #setItemGravity(int)
498+
*/
499+
public int getItemGravity() {
500+
return itemGravity;
501+
}
502+
478503
/**
479504
* Get the width of the selected item's active indicator when expanded.
480505
*
@@ -886,6 +911,7 @@ public void buildMenuView() {
886911
child.setActiveIndicatorExpandedWidth(itemActiveIndicatorExpandedWidth);
887912
child.setActiveIndicatorExpandedHeight(itemActiveIndicatorExpandedHeight);
888913
child.setActiveIndicatorMarginHorizontal(itemActiveIndicatorMarginHorizontal);
914+
child.setItemGravity(itemGravity);
889915
child.setActiveIndicatorExpandedMarginHorizontal(itemActiveIndicatorExpandedMarginHorizontal);
890916
child.setActiveIndicatorDrawable(createItemActiveIndicatorDrawable());
891917
child.setActiveIndicatorResizeable(itemActiveIndicatorResizeable);
@@ -946,6 +972,7 @@ public void updateMenuView() {
946972
presenter.setUpdateSuspended(true);
947973
buttons[i].setLabelVisibilityMode(labelVisibilityMode);
948974
buttons[i].setItemIconGravity(itemIconGravity);
975+
buttons[i].setItemGravity(itemGravity);
949976
buttons[i].setShifting(shifting);
950977
buttons[i].initialize((MenuItemImpl) menu.getItem(i), 0);
951978
presenter.setUpdateSuspended(false);

lib/java/com/google/android/material/navigation/NavigationBarView.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import androidx.appcompat.view.menu.MenuView;
3737
import androidx.appcompat.widget.TintTypedArray;
3838
import android.util.AttributeSet;
39+
import android.view.Gravity;
3940
import android.view.Menu;
4041
import android.view.MenuInflater;
4142
import android.view.MenuItem;
@@ -131,6 +132,26 @@ public abstract class NavigationBarView extends FrameLayout {
131132
/** Icon is placed at the top of the item */
132133
public static final int ITEM_ICON_GRAVITY_START = 1;
133134

135+
/**
136+
* Navigation Bar Item gravity enum to control where the item is in its container.
137+
*
138+
* @hide
139+
*/
140+
@RestrictTo(LIBRARY_GROUP)
141+
@IntDef(value = {ITEM_GRAVITY_TOP_CENTER, ITEM_GRAVITY_CENTER, ITEM_GRAVITY_START_CENTER})
142+
@Retention(RetentionPolicy.SOURCE)
143+
public @interface ItemGravity {}
144+
145+
/** Item is placed at the top center of its container */
146+
public static final int ITEM_GRAVITY_TOP_CENTER = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
147+
148+
/** Item is placed at the center of its container */
149+
public static final int ITEM_GRAVITY_CENTER = Gravity.CENTER;
150+
151+
/** Item is placed at the start center of its container */
152+
public static final int ITEM_GRAVITY_START_CENTER = Gravity.START | Gravity.CENTER_VERTICAL;
153+
154+
134155
/**
135156
* Navigation Bar Item icon gravity enum to control which item configuration to display.
136157
*
@@ -274,6 +295,9 @@ public NavigationBarView(
274295
attributes.getInteger(
275296
R.styleable.NavigationBarView_itemIconGravity,
276297
NavigationBarView.ITEM_ICON_GRAVITY_TOP));
298+
setItemGravity(
299+
attributes.getInteger(
300+
R.styleable.NavigationBarView_itemGravity, NavigationBarView.ITEM_GRAVITY_TOP_CENTER));
277301

278302
int itemBackground = attributes.getResourceId(R.styleable.NavigationBarView_itemBackground, 0);
279303
if (itemBackground != 0) {
@@ -740,6 +764,29 @@ public void setItemActiveIndicatorMarginHorizontal(@Px int horizontalMargin) {
740764
menuView.setItemActiveIndicatorMarginHorizontal(horizontalMargin);
741765
}
742766

767+
/**
768+
* Sets the navigation items' layout gravity.
769+
*
770+
* @param itemGravity the layout {@link android.view.Gravity} of the item
771+
* @see #getItemGravity()
772+
*/
773+
public void setItemGravity(@ItemGravity int itemGravity) {
774+
if (menuView.getItemIconGravity() != itemGravity) {
775+
menuView.setItemGravity(itemGravity);
776+
presenter.updateMenuView(false);
777+
}
778+
}
779+
780+
/**
781+
* Returns the navigation items' layout gravity.
782+
*
783+
* @see #setItemGravity(int)
784+
*/
785+
@ItemGravity
786+
public int getItemGravity() {
787+
return menuView.getItemGravity();
788+
}
789+
743790
/**
744791
* Get the width of an item's active indicator when it is expanded to wrap the item content, ie.
745792
* when it is in the {@link ItemIconGravity#ITEM_ICON_GRAVITY_START} configuration.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<public name="itemPaddingBottom" type="attr"/>
5757
<public name="itemActiveIndicatorStyle" type="attr"/>
5858
<public name="itemIconGravity" type="attr"/>
59+
<public name="itemGravity" type="attr"/>
5960
<public name="marginHorizontal" type="attr"/>
6061
<public name="expandedWidth" type="attr"/>
6162
<public name="expandedHeight" type="attr"/>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
<enum name="start" value="1"/>
8989
</attr>
9090

91+
<!-- The gravity of the item within the Navigation Bar -->
92+
<attr name="itemGravity">
93+
<!-- Gravity.TOP | Gravity.CENTER_HORIZONTAL -->
94+
<enum name="top_center" value="49"/>
95+
<!-- Gravity.CENTER-->
96+
<enum name="center" value="17"/>
97+
<!-- Gravity.START | Gravity.CENTER_VERTICAL -->
98+
<enum name="start_center" value="8388627"/>
99+
</attr>
100+
91101
<!-- Measure the item's bottom padding from the baseline of the label, not the bottom of its
92102
view. -->
93103
<attr name="measureBottomPaddingFromLabelBaseline" format="boolean"/>

0 commit comments

Comments
 (0)