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

Bottom App Bar未显示Search图标,仅在溢出菜单中显示问题求助

解决Bottom App Bar中Search图标未直接显示的问题

嘿,我来帮你排查下这个问题!你的Search图标没显示在Bottom App Bar上,而是跑到溢出菜单里,主要有几个关键原因,咱们一步步解决:

1. 调整MenuItem的优先级(最核心的问题)

你给Search和Settings的orderInCategory都设成了100,这个值越高,item的优先级越低。BottomAppBar的空间有限,高优先级的item才会优先显示在栏上,低优先级的会被挤到溢出菜单。

把Search的orderInCategory改成更小的数值(比如1),提升它的优先级:

<item android:title="@string/search"
      android:id="@+id/search"
      android:orderInCategory="1" <!-- 降低数值提升优先级 -->
      android:icon="@drawable/ic_search_black_24dp"
      app:showAsAction="always" /> <!-- 改用app命名空间的属性更稳妥 -->

同时保留Settings的orderInCategory="100",让它继续留在溢出菜单里。

2. 检查Fab相关配置对空间的影响

你设置了app:fabAttached="true"和app:fabAlignmentMode="center",这会让BottomAppBar中间预留出Fab的位置,占用了一部分横向空间。如果你的布局里并没有实际添加Fab(或者你不需要Fab),可以把app:fabAttached改成false,这样底部栏的可用空间会更充足:

<com.google.android.material.bottomappbar.BottomAppBar
    style="@style/Widget.MaterialComponents.BottomAppBar"
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_gravity="bottom"
    android:elevation="5dp"
    app:elevation="5dp"
    app:fabAttached="false" <!-- 不需要Fab就关闭这个属性 -->
    app:navigationIcon="@drawable/ic_bottom_bar_hamburger"
    app:backgroundTint="@color/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:fabAlignmentMode="center"/>

如果确实需要Fab,记得在布局中添加对应的FloatingActionButton,并调整app:fabCradleDiameter为合适的数值(不要设为0dp,这会导致预留空间异常)。

3. 统一使用app命名空间的showAsAction属性

Material Components的组件对app:前缀的属性支持更好,建议把android:showAsAction改成app:showAsAction,确保属性能被正确识别。

修改后的完整代码示例

BottomAppBar布局:

<com.google.android.material.bottomappbar.BottomAppBar
    style="@style/Widget.MaterialComponents.BottomAppBar"
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_gravity="bottom"
    android:elevation="5dp"
    app:elevation="5dp"
    app:fabAttached="false"
    app:navigationIcon="@drawable/ic_bottom_bar_hamburger"
    app:backgroundTint="@color/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:fabAlignmentMode="center"/>

Menu Items:

<item android:id="@+id/action_settings"
      android:orderInCategory="100"
      android:title="@string/action_settings"
      app:showAsAction="never" />

<item android:title="@string/search"
      android:id="@+id/search"
      android:orderInCategory="1"
      android:icon="@drawable/ic_search_black_24dp"
      app:showAsAction="always" />

按照上面的修改调整后,Search图标应该就能正常显示在Bottom App Bar上了!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:08:35