我需要添加一个计数器到NavigationDrawer的字段nav_films_been_wathced
,但我找不到如何正确地做到这一点。我用com.android.support:design:23.0.1
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
我menu。xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/group_top"
android:checkableBehavior="single">
<item
android:id="@+id/nav_main"
android:icon="@drawable/ic_home"
android:title="@string/nav_main" />
<item
android:id="@+id/nav_films_been_wathced"
android:icon="@drawable/ic_films"
android:title="@string/nav_films_been_wathced" />
</group>
<group
android:id="@+id/group_bottom"
android:checkableBehavior="none">
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info"
android:title="@string/nav_about" />
</group>
</menu>
在修订版23.0.1中,您可以向导航菜单项添加actionView。像这样做:
<item
android:id="@+id/example_id"
app:actionViewClass="android.widget.TextView"
app:showAsAction="always"
android:icon="@drawable/example_drawable"
android:title="@string/example_title" />
你可以像这样获得对你的动作视图的引用:
TextView actionView = (TextView) MenuItemCompat.getActionView(mNavigationView.getMenu().findItem(R.id.example_id));
从那里你可以把它当作任何普通的textview:-)
下面是一个预览:
https://drive.google.com/a/bubblebearapps.co.uk/file/d/0B2uf1i54j9TIcUVVYTlfcFlXdmM/view?usp=sharing