Material 3 Bottom App Bar居中FAB时图标左移问题求助
Material 3 BottomAppBar 图标位置偏移问题
我在Material 2环境下,通过以下布局实现了Bottom App Bar:
<com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bar" style="@style/Widget.MaterialComponents.BottomAppBar.Colored" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:backgroundTint="@color/primarycolor" app:fabAlignmentMode="center" app:navigationIcon="@drawable/ic_menu_time" app:navigationIconTint="@color/lighttextcolor2" app:theme="@style/ThemeOverlay.MaterialComponents.Dark" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:backgroundTint="@color/fab" app:layout_anchor="@id/bar" app:rippleColor="#FFFF8888" app:srcCompat="@drawable/ic_fab_search" app:tint="@color/darktextcolor" />
切换为Material 3的style="@style/Widget.Material3.BottomAppBar"后,虽然获得了M3的全新设计,但菜单图标却偏移到了左侧。尝试在menu.xml中添加android:layout_alignParentRight="true",但没有任何效果,求可行的解决办法。
解决步骤
使用Material 3专属属性控制菜单位置
Material 3的BottomAppBar新增了app:menuGravity属性,直接将其设置为end,就能让菜单图标固定在右侧:<com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bar" style="@style/Widget.Material3.BottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:backgroundTint="@color/primarycolor" app:fabAlignmentMode="center" app:navigationIcon="@drawable/ic_menu_time" app:navigationIconTint="@color/lighttextcolor2" app:menu="@menu/your_bottom_menu" app:menuGravity="end" /> <!-- 核心配置 -->放弃无效的RelativeLayout属性
android:layout_alignParentRight是RelativeLayout的布局属性,不适用于BottomAppBar的菜单布局逻辑,无需再尝试这个方法。检查自定义菜单布局(如有)
如果你的menu.xml中为菜单项设置了自定义布局,确保自定义布局根视图的android:layout_gravity没有硬设为start;不过优先通过BottomAppBar的menuGravity统一控制,能避免局部配置冲突。
内容的提问来源于stack exchange,提问作者Waza_Be
相关产品推荐
相关产品推荐

