Navigation drawer menu sub categories

时间:2015-07-08 15:45:57

标签: java android xml android-layout android-navigation

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?

2 个答案:

答案 0 :(得分:5)

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!

答案 1 :(得分:0)

我认为此代码可以帮助您在项目和子项目之间设置分隔符。看看这个:

<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>

快乐编码