如何在NavigationView中的项目上添加徽章

时间:2015-08-09 18:39:19

标签: android navigation-drawer

我刚刚开始使用新的组件android.support.design.widget.NavigationView,在此之前我使用标准列表视图导航抽屉但是现在我开始使用新的组件导航视图并且有问题在项目上实现徽章。 现在有人如何解决这个问题?

2 个答案:

答案 0 :(得分:8)

这可以通过以下步骤完成

<强> 1。添加&#34; actionViewClass&#34;属性导航抽屉菜单

使用导航抽屉创建“Helloworld”应用程序后,在项目层次结构视图的“菜单”文件夹下查找文件“activity_main_drawer.xml”(即youractivityname_drawer.xml)。 识别组项并添加“app:actionViewClass = android.widget.TextView”,如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import" />
    <item
        android:id="@+id/nav_gallery"
        app:actionViewClass="android.widget.TextView"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        app:actionViewClass="android.widget.TextView"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<强> 2。声明导航抽屉菜单项并使用徽章值初始化项目。

在您的主Activity中,声明导航抽屉的菜单项,如下所示

//Create these objects above OnCreate()of your main activity
TextView slideshow,gallery;

//These lines should be added in the OnCreate() of your main activity
gallery=(TextView) MenuItemCompat.getActionView(navigationView.getMenu().findItem(R.id.nav_gallery));
slideshow=(TextView) MenuItemCompat.getActionView(navigationView.getMenu().findItem(R.id.nav_slideshow));

//This method will initialize the count value
initializeCountDrawer();

initializeCountDrawer()可以在需要的地方调用。它还可用于更新导航抽屉菜单项中的计数或徽章值。

private void initializeCountDrawer() {
    //Gravity property aligns the text
    gallery.setGravity(Gravity.CENTER_VERTICAL);
    gallery.setTypeface(null, Typeface.BOLD);
    gallery.setTextColor(getResources().getColor(R.color.colorAccent));
    gallery.setText("99+");
    slideshow.setGravity(Gravity.CENTER_VERTICAL);
    slideshow.setTypeface(null,Typeface.BOLD);                
    slideshow.setTextColor(getResources().getColor(R.color.colorAccent));

    //count is added     
    slideshow.setText("7");
}

在添加上述方法时,请运行该应用。瞧瞧! 一个简单的徽章计数将显示在导航抽屉的“图库”和“幻灯片”菜单项上。

动态徽章值

如果您需要动态徽章值,例如更新API调用或SQLite数据库中的值,请创建可重用的方法并在Activity的OnStart()或OnResume()方法上更新它。

可以找到完整的源代码here

答案 1 :(得分:2)

您仍然必须使用ListView来设置布局。

为了使用NavigationView属性,我的解决方法是将具有不同背景的SpannableString作为MenuItem的新标题传递。

我知道这不是最好的解决方案,但它可以很好地用作计数器。像这样:

NavigationView navigation = (NavigationView)findViewById(R.id.navigation);
Menu menuNav = navigation.getMenu();
MenuItem element = menuNav.findItem(R.id.item5);
String before = element.getTitle().toString();

String counter = Integer.toString(5);
String s = before + "   "+counter+" ";
SpannableString sColored = new SpannableString( s );

sColored.setSpan(new BackgroundColorSpan( Color.GRAY ), s.length()-3, s.length(), 0);
sColored.setSpan(new ForegroundColorSpan( Color.WHITE ), s.length()-3, s.length(), 0);


element.setTitle(sColored);