将MenuItem添加到Menu中的特定位置或以编程方式进行分组 [英] Add a MenuItem to Menu at specific position or group programmatically

查看:96
本文介绍了将MenuItem添加到Menu中的特定位置或以编程方式进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

<android.support.design.widget.NavigationView
  app:menu="@menu/drawer"
/>

具有以下菜单项:

<item
    android:id="@+id/main_item"
    android:icon="@drawable/ic_menu_main"
    android:title="@string/app_name"/>

<group
    android:id="@+id/some_group"
    android:checkableBehavior="single"/>

<item
    android:id="@+id/teams_item"
    android:icon="@drawable/ic_menu_teams"
    android:title="@string/teams"/>

现在,我想向some_group或正下方添加一个项目.

Now I want to add an item either to the some_group or just below it.

我尝试过:

MenuItem mi = menu.add( R.id.soume_group, someId, NONE, "some name" );

MenuItem mi = menu.add( R.id.soume_group, someId, 2, "some name" );

,但项目添加在菜单的底部.

but the items are added at the bottom of the menu.

如何解决我的问题?

TIA

推荐答案

您可以使用orderInCategory定义顺序

    <item
        android:id="@+id/main_item"
        android:icon="@drawable/ic_menu_main"
        android:orderInCategory="100"   
        android:title="@string/app_name"/>

    <item
        android:id="@+id/teams_item"
        android:icon="@drawable/ic_menu_teams"
        android:orderInCategory="1000"      
        android:title="@string/teams"/>

如果要在main_itemteams_item之间插入,可以使用介于orderInCategoryorderInCategory之间的

if you want to insert between main_item and teams_item you can use orderInCategory that is between

// 500 is between main_item(100) and teams_item(1000)
MenuItem mi = menu.add( NONE, someId, 500, "some name" ); 

这篇关于将MenuItem添加到Menu中的特定位置或以编程方式进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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