如何得到NavigationView菜单分隔无标题? [英] How to get dividers in NavigationView menu without titles?

查看:219
本文介绍了如何得到NavigationView菜单分隔无标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是新的 NavigationView 来创建XML我的抽屉式导航菜单。我需要放置部分的菜单项,其中我的应用程序的各部分之间进行切换,且该设置和帮助&安培之间的分隔;支持链接的下方。

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.

在所有我见过的例子,我看到这可以通过将另一个&LT完成;菜单> <项目&GT ; ,而<项目> 要求有安卓标题属性,所以最好我能做的就是使标题空白,之前设置,这留下一个空的空间,并帮助和放大器;反馈。

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>

我已经试过&LT的各种组合;菜单&gt; &LT;项目&GT; &LT;组&gt; 标签,但没有发现任何东西,将工作。例如,这有使用previous组的组标题中的最后一个项目的问题:

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>

目前只是有一个简单的方法只使用菜单的XML描述做到这一点。谷歌已经在其<一本非常行为href="http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-content">Material设计规范。

编辑:

然而,另一个靠近的尝试:

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>

此叶以上和以下的分压器的项之间没有空间,但仍然在顶部空间现在。此外,安卓checkableBehavior =单行为异常。当选择了第一时间,一旦其他人变成选中的项目未选中的物品都没有被选中。

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.

推荐答案

从:<一href="http://stackoverflow.com/questions/30605286/navigationview-insert-divider-without-subgroup">NavigationView插入分隔不群

看起来你只是需要给你的标签的唯一ID的。

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>

作为回答说:

[NavigationView]每个组ID改变时会创建一个分压器

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

这篇关于如何得到NavigationView菜单分隔无标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆