You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BottomAppBar添加选项菜单后导航图标未垂直居中问题求助

Fix for BottomAppBar Navigation Icon Offset After Adding Menu Items

Hey there, I've run into this exact navigation icon alignment issue with android.support.design.bottomappbar.BottomAppBar before—super frustrating when the icon shifts left after adding menu items! Here are a few tested solutions that should fix it:

1. Force Vertical Centering via Code

You can directly target the navigation icon's view and adjust its layout parameters to enforce vertical centering, even after the menu loads:

Kotlin Code:

val navigationIconView = bottomAppBar.findViewById<View>(com.google.android.material.R.id.navigation_icon)
val params = navigationIconView.layoutParams as ViewGroup.MarginLayoutParams
params.gravity = Gravity.CENTER_VERTICAL
navigationIconView.layoutParams = params

Java Code:

View navigationIconView = bottomAppBar.findViewById(com.google.android.material.R.id.navigation_icon);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) navigationIconView.getLayoutParams();
params.gravity = Gravity.CENTER_VERTICAL;
navigationIconView.setLayoutParams(params);

Run this code right after you inflate your menu (inside onCreateOptionsMenu or after calling inflateMenu() on the BottomAppBar).

2. Adjust Menu Item Display Behavior

If your menu items are set to showAsAction="always", they might be taking up too much horizontal space and pushing the navigation icon out of alignment. Try modifying your menu XML to use showAsAction="ifRoom" instead for non-critical items:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="Search"
        app:showAsAction="ifRoom"/> <!-- Use ifRoom instead of always -->
</menu>

This prevents the menu items from forcing the navigation icon's container to shrink unexpectedly.

3. Update Material Components Library

Older versions of the Material Components library had layout bugs with BottomAppBar's navigation icon alignment. Make sure you're using the latest stable version in your build.gradle (Module level):

dependencies {
    implementation 'com.google.android.material:material:1.11.0' // Or newer
}

Upgrading often resolves subtle layout glitches like this one.

4. Check Navigation Icon Drawable Dimensions

Sometimes the issue is with the navigation icon itself—if it has uneven padding or an odd aspect ratio, it might shift when the menu loads. Try using a drawable with consistent dimensions, or add explicit padding to the icon via XML:

<android.support.design.bottomappbar.BottomAppBar
    android:id="@+id/bottom_app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:navigationIcon="@drawable/ic_menu_24dp" <!-- Ensure this drawable is properly sized -->
    app:paddingStart="8dp"
    app:paddingEnd="8dp"/>

Why This Happens

Under the hood, the BottomAppBar uses a linear layout to arrange the navigation icon, menu items, and FAB (if present). When menu items are added, the layout recalculates weights and positions—older implementations didn't properly preserve the navigation icon's vertical alignment in this scenario.

内容的提问来源于stack exchange,提问作者Neeraj Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:31:49