首页 > 解决方案 > 将徽章添加到底部导航并且无法引用 getOrCreateBadge

问题描述

我正在尝试在底部导航中添加徽章,但问题是 getOrCreateBadge 没有被引用。我无法访问此方法。

错误:未解决的参考:getOrCreateBadge

 <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">


   <androidx.fragment.app.FragmentContainerView
       android:id="@+id/fragment_container"
       android:name="androidx.navigation.fragment.NavHostFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_above="@+id/bnv_nav"
       app:defaultNavHost="true"
       app:layout_behavior="@string/appbar_scrolling_view_behavior" />



   <com.google.android.material.bottomnavigation.BottomNavigationView
       android:id="@+id/bnv_nav"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:minHeight="@dimen/dp56"
       android:layout_alignParentBottom="true"
       android:fitsSystemWindows="true"
       app:itemIconTint="@drawable/tab_color"
       app:itemTextColor="@drawable/tab_color" />

主题

<style name="AppTheme" parent="Theme.AppCompat.Light">
       <item name="colorPrimary">@color/white</item>
       <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
       <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
       <item name="android:navigationBarColor" tools:targetApi="21">@color/white</item>
       <item name="android:windowLightNavigationBar" tools:targetApi="27">true</item>
       <item name="android:windowDisablePreview">true</item>
   </style>

我在运行时添加菜单项。

我已经尝试过的事情:

  1. 从 xml 本身添加菜单项:仍然没有效果
  2. 将主题更改为材料主题:仍然是同样的问题
  3. 使用底部导航活动创建了一个单独的项目:如果我添加徽章,该项目中没有问题。我也将它的主题更改为我的应用程序主题,即应用程序兼容仍然徽章在该项目中运行良好。
  4. 最初没有添加android材料的依赖,我可以使用底部导航栏。但是由于我在添加徽章时遇到了问题,所以我在外部添加了依赖项,并确保也放入最新版本,但最终仍然面临同样的问题。无法在底部导航中添加徽章。

我知道这是非常基本的事情,但如果有人能指出这将是一个很大的帮助,这将是一个很大的帮助,从过去两天开始就已经解决了这个问题。预先感谢。

标签: androidmaterial-designbottomnavigationviewbadge

解决方案


理想情况下,您应该使用实现 'com.google.android.material:material:1.3.0' 。确保在 gradle 同步和重建之前删除 builds 文件夹。


推荐阅读