android 在底部导航视图上删除/隐藏所选项目标题

vuv7lop3  于 2023-06-20  发布在  Android
关注(0)|答案(2)|浏览(131)

我需要创建一个自定义的底部导航视图。情况是当我点击任何项目时,标签都消失了,只显示图标。示例图像为

我试图创建一个自定义使用选择器一样

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item
  3. android:id="@+id/tested"
  4. android:icon="@drawable/selector_nav_tested"
  5. android:title="tested"/>
  6. </menu>

但它只是改变了图标。如果我使用labelVisibilityMode = LABEL_VISIBILITY_UNLABELED,标签不会显示在任何导航项上。有没有办法通过在setOnNavigationItemSelectedListener中添加代码来自定义它,或者我需要使用库?

mdfafbf1

mdfafbf11#

这是默认情况下完成的。您可以使用

  1. app:labelVisibilityMode="labeled"

在你的BottomNavigationView上,它应该强制覆盖它以显示所有标签。

  1. app:labelVisibilityMode="auto"
  2. app:labelVisibilityMode="labeled"
  3. app:labelVisibilityMode="unlabeled"

以上三点是要保持的要点。自动是点击时的隐藏。有标签的总是有标签的,无标签的是没有标签的。
希望这能帮上忙。

pnwntuvh

pnwntuvh2#

labelVisibilityMode属性可用于调整每个导航项的文本标签的行为。有四种可见性模式:

LABEL_VISIBILITY_AUTO默认:当有3个或更少的项目时,标签表现为“已标记”,当有4个或更多的项目时,标签表现为“已选择”
LABEL_VISIBILITY_SELECTED:标签仅显示在选定的导航项上
LABEL_VISIBILITY_LABELED:标签显示在所有导航项上
LABEL_VISIBILITY_UNLABELED:对于所有导航项,标签都是隐藏的

  1. <com.google.android.material.bottomnavigation.BottomNavigationView
  2. android:id="@+id/bottom_navigation"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. **app:labelVisibilityMode="unlabeled"**
  6. app:menu="@menu/bottom_navigation_menu" />

相关问题