How to get dividers in NavigationView menu without

2019-02-03 23:24发布

问题:

I am using the new NavigationView to create my navigation drawer menu from XML. I need to place a divider between the section menu items, which switch between the sections of my app, and the settings and help & support links at the bottom.

In all the examples I've seen, I see how this can be done by putting another <menu> within an <item>, but the <item> requires to have the android:title attribute, so the best I can do is make the title blank, which leaves an empty space before the settings and help & feedback.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_section_1"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_1"
            android:checked="true" /> <!-- default selection -->
        <item
            android:id="@+id/nav_section_2"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_2" />
        <item
            android:id="@+id/nav_section_3"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_3" />
    </group>

    <item android:title="@null"> <!-- I don't want a title or space here! -->
        <menu>
            <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_settings"
                android:title="@string/settings" />
            <item
                android:id="@+id/nav_help_feedback"
                android:icon="@drawable/ic_help"
                android:title="@string/help_feedback" />
        </menu>
    </item>
</menu>

I've tried various combinations of <menu>, <item> and <group> tags, but haven't found anything that will work. This for example has the issue of using the last item in the previous group as the group title:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_section_1"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_1"
            android:checked="true" /> <!-- default selection -->
        <item
            android:id="@+id/nav_section_2"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_2" />
        <item
            android:id="@+id/nav_section_3"
            android:icon="@drawable/ic_dashboard"
            android:title="@string/section_3" />
    </group>

    <group> <!-- This puts @string/section_3 as the group title! -->
        <menu>
            <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_settings"
                android:title="@string/settings" />
            <item
                android:id="@+id/nav_help_feedback"
                android:icon="@drawable/ic_help"
                android:title="@string/help_feedback" />
        </menu>
    </item>
</menu>

There just has to be an easy way to do this using just the menu XML description. Google has this very behavior in their Material design spec.

EDIT:

Yet another close attempt:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="@null"> <!-- Still a space here though! -->
        <menu>
            <group android:checkableBehavior="single"> <!-- And this checkable behavior behaves strangely for some reason -->
                <item
                    android:id="@+id/nav_section_1"
                    android:icon="@drawable/ic_dashboard"
                    android:title="@string/section_1"
                    android:checked="true" /> <!-- default selection -->
                <item
                    android:id="@+id/nav_section_2"
                    android:icon="@drawable/ic_dashboard"
                    android:title="@string/section_2" />
                <item
                    android:id="@+id/nav_section_3"
                    android:icon="@drawable/ic_dashboard"
                    android:title="@string/section_3" />
            </group>
        </menu>
    </item>

    <group> <!-- Finally, no space or title here! -->
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings" />
        <item
            android:id="@+id/nav_help_feedback"
            android:icon="@drawable/ic_help"
            android:title="@string/help_feedback" />
    </item>
</menu>

This leaves no space between the items above and below the divider, but there's still the space at the top now. Also, the android:checkableBehavior="single" behaves strangely. Items are not selected when selected the first time and items are not unselected once others do become selected.

回答1:

From: NavigationView: how to insert divider without subgroup?

It looks like you just need to give your group tags unique ID's.

<group android:id="@+id/my_id">
    <!-- Divider will appear above this item -->
    <item ... />
</group>

As the answer says:

[NavigationView] will create a divider every time the group id is changed



回答2:

This is exact solution for your question here.

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

    <group
        android:id="@+id/menu_top"
        android:checkableBehavior="single">
        <item android:title="Switch Team">
            <menu>
                <item
                    android:id="@+id/team"
                    android:title=""
                    app:actionLayout="@layout/layout_spinner_for_drawer"/>
            </menu>
        </item>
    </group>

    <group
        android:id="@+id/menu_bottom"
        android:checkableBehavior="single">
            <item
                android:id="@+id/nav_home"
                android:icon="@drawable/home"
                android:title="Home" />
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/sharebox"
                android:title="Sharebox" />
            <item
                android:id="@+id/nav_recognize"
                android:icon="@drawable/recognize"
                android:title="Recognize" />
            <item
                android:id="@+id/nav_contact_us"
                android:icon="@drawable/contactus"
                android:title="Contact Us" />
            <item
                android:id="@+id/nav_logout"
                android:icon="@drawable/signout"
                android:title="Logout" />
    </group>
</menu>