android - BottomAppBar 中未显示菜单项文本
问题描述
我一直在尝试将菜单项的标题与 Android Studio 底部应用栏中的菜单图标一起添加。我试图设置:
app:showAsAction="ifRoom|withText"
但它没有用。仅显示图标。我还尝试制作自己的布局文件并使用 actionLayout:
android:actionLayout="@layout/menu_item_lists"
这也不起作用。
我想要的示例(图标下方的文本):
现在的样子:
我的活动的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ededed"
tools:context=".MainActivity">
<fragment
android:id="@+id/activity_main_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#ffffff"
app:fabAlignmentMode="end"
app:fabCradleMargin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/bottomAppBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:rippleColor="@color/colorAccent"
app:srcCompat="@drawable/ic_add_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
和我的菜单代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/fragment_menu_lists"
android:title="Handlelister"
android:icon="@drawable/ic_format_list_bulleted_black_24dp"
android:actionLayout="@layout/menu_item_lists"
app:showAsAction="ifRoom|withText" />
<item android:id="@+id/fragment_menu_stores"
android:title="Butikker"
android:icon="@drawable/ic_map_black_24dp"
app:showAsAction="ifRoom|withText"/>
<item android:id="@+id/fragment_menu_profile"
android:title="Profil"
android:icon="@drawable/ic_person_outline_black_24dp"
app:showAsAction="ifRoom|withText"
/>
</menu>
如果相关,我尝试创建自己的操作布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_format_list_bulleted_black_24dp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Handlelister"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
毕竟我想出了一个办法:
而不是使用
android:actionLayout="@layout/menu_item_lists"
我用了:
app:actionLayout="@layout/menu_item_lists"
然后我不得不在菜单项上添加 onClickListener:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu_bottom_navigation, menu)
val navController = findNavController(R.id.activity_main_nav_host_fragment)
menu?.findItem(R.id.fragment_menu_lists)?.actionView?.setOnClickListener {
navController.navigate(R.id.fragment_menu_lists)
}
menu?.findItem(R.id.fragment_menu_stores)?.actionView?.setOnClickListener {
navController.navigate(R.id.fragment_menu_stores)
}
menu?.findItem(R.id.fragment_menu_profile)?.actionView?.setOnClickListener {
navController.navigate(R.id.fragment_menu_profile)
}
return super.onCreateOptionsMenu(menu)
}
推荐阅读
- android - 编译 tensorflow lite 最小示例
- javascript - 如何在 React 中使用带有钩子的生命周期方法?
- git - 始终创建新的 Git 分支,即使是微小的/微不足道的更改
- python - 如何在 pandas 数据框中使用单引号将一列拆分为两列?
- powershell - 需要 Powershell 脚本从目录和子目录中的 *.msg 文件中删除所有空格
- python - 类中的类似方法
- javascript - 单击一个元素会触发所有相同的类,而不是单击的那个
- adobe-xd - 我正在全局更改打开的 XD 文档,但收到以下错误
- swift - 使用 CABasicAnimation 将半径从 0 调整为 100
- ms-access - MS Access - 将子报表链接到主报表时链接主/子字段不起作用