简体   繁体   中英

How to show an indicator with a navigation drawer menu item

I am trying to show a count indicator with some of the associated menu items. How do I go about doing that? I tried creating TextView children for menu items, but they don't seem to work. Here's a mockup of what I am trying to achieve (see notifier next to the Gallery option)

在此处输入图片说明

If you are using NavigationView

NavigationView provides a convenient way to build a navigation drawer, including the ability to creating menu items using a menu XML file. We've expanded the functionality possible with the ability to set custom views for items via app:actionLayout or using MenuItemCompat.setActionView().


Example

<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"
    app:menu="@menu/drawer_menu" />

drawer_menu.xml

<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_item1"
            app:showAsAction="always"
            android:title=""
            app:actionLayout="@layout/menu_item_layout" />
    </group>
</menu>

menu_item_layout.xml

you can customize as you want

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Item text" />
</RelativeLayout>

you can create FrameLayout in which add your Titile TextView and above that your Count TextView as follows t

<FrameLayout>
    <TextView></TextView> //your titile TextView
    <TextView></TextView> //your count TextView which has gravity of top right and circular background drawable
</FrameLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM