Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

android - How to get dividers in NavigationView menu without titles?

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.

Settings and support

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.

question from:https://stackoverflow.com/questions/30790420/how-to-get-dividers-in-navigationview-menu-without-titles

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...