简体   繁体   中英

Navigation drawer menu sub categories

I have a Navigation Drawer in my app using the DrawerLayout class and NavigationView. Right now I have my drawer showing all of my categories by inflating a menu xml file. How can I get it so that some of these categories are separated by subheadings and dividers?

You can do this very easily by just modifying your existing menu xml file.

Your code will look something like this:

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

    <item
        android:id="@+id/group1"
        android:title="title1">
        <menu>
            <item
                android:title="item1" />
            <item
                android:title="item2" />
        </menu>
    </item>

    <item
        android:id="@+id/group2"
        android:title="title2">
        <menu>
            <item
                android:title="item1" />
            <item
                android:title="item2" />
            <item
                android:title="item3" />
        </menu>
    </item>
</menu>

All you do is specify a new menu within each item and give the parent item a title!

I think this code will help you to setup a divider between your items and sub items. Check this out:

<menu 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"
tools:context=".MainActivity">

<group android:id="@+id/grp1" android:checkableBehavior="single" >
    <item
        android:id="@+id/navigation_item_1"
        android:checked="true"
        android:icon="@drawable/ic_home"
        android:title="@string/navigation_item_1" />
</group>

<group android:id="@+id/grp2" android:checkableBehavior="single" >
    <item
        android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_home"
        android:title="@string/navigation_item_2" />
</group>

Happy Coding

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