我正在使用不久前刚刚在 I/O 上展示过的新 Material Components。我能够创建底部应用栏,但菜单不显示搜索图标。相反,它只显示在溢出菜单中。我做错了什么?
<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"
app:elevation="5dp"
android:elevation="5dp"
app:fabAttached="true"
app:fabCradleDiameter="0dp"
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"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never" />
<item
android:title="@string/search"
android:id="@+id/search"
android:orderInCategory="100"
android:icon="@drawable/ic_search_black_24dp"
android:showAsAction="always" />
最佳答案
- 如果您还没有创建一个菜单资源目录。右键
res>>新建>>android资源目录>>在资源类型中选择菜单
- 创建新的菜单文件。右键单击新建的菜单目录
New Menu Resource file
将其命名为bottom_bar_menu
- 将您的项目放入
bottom_bar_menu
- 在 xml 中的
BottomBar
中使用app:menu="@menu/bottom_bar_menu"
。
所以你最终的 xmls 会像:
<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"
app:elevation="5dp"
android:elevation="5dp"
app:fabAttached="true"
app:fabCradleDiameter="0dp"
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"
app:menu="@menu/bottom_bar_menu"/>
然后在 res>menu>bottom_bar_menu
中,将 showAsAction
更改为 always
或 ifRoom
,为action_settings
并移除 orderInCategory
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="always"
android:icon="" />
<item
android:title="@string/search"
android:id="@+id/search"
android:icon="@drawable/ic_search_black_24dp"
android:showAsAction="always" />
</menu>
关于android - 底部应用栏不显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432059/