如何以编程方式将子菜单项添加到新的材质设计android支持库 [英] How to programmatically add a submenu item to the new material design android support library

查看:88
本文介绍了如何以编程方式将子菜单项添加到新的材质设计android支持库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新的Material Design抽屉面板的子菜单中添加新的菜单项.

I am trying to add new menu items to a submenu in the new Material Design drawer panel.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

...

    <android.support.design.widget.NavigationView
        android:id="@+id/main_navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/channel_list_header"
        app:menu="@menu/menu_nav" /> 

</android.support.v4.widget.DrawerLayout>

我确实有@ menu/menu_nav设置,但现在想添加新的菜单项:

I do have the @menu/menu_nav setup but now want to add new menu items:

NavigationView mDrawerList = (NavigationView) findViewById(R.id.main_navigation);

Menu menu = mDrawerList.getMenu();
Menu topChannelMenu = menu.addSubMenu("Top Channels");

topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");

mDrawerList.invalidate(); // Trying to force a redraw, doesn't help.

显示了@ menu/menu_nav文件中的菜单项,但没有显示新添加的菜单项. 任何想法如何做到这一点?

The menu items from the @menu/menu_nav file are shown, but the newly added ones aren't. Any ideas how to do that?

推荐答案

[更新20-03-2016] 错误已解决.因此,无需担心.

[Update 20-03-2016] Bug is resolved. So no need to worry.

[以下竞争内容已过时.]

将动态菜单添加到NavigationView当前是设计支持库中的错误.并且我已将其报告给android bug源跟踪.因此,请等待该错误修复.但是,如果您想要临时解决方案,则可以这样做.首先添加动态菜单..

Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I have report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..

    navView = (NavigationView) findViewById(R.id.navView);
    Menu m = navView.getMenu();
    SubMenu topChannelMenu = m.addSubMenu("Top Channels");
    topChannelMenu.add("Foo");
    topChannelMenu.add("Bar");
    topChannelMenu.add("Baz");

添加菜单后,只需在下面的代码中写上..

After adding your menu just write below code ..

    MenuItem mi = m.getItem(m.size()-1);
    mi.setTitle(mi.getTitle());

这是目前很棘手的解决方案.但是为我工作...

It's currently hacky solution. But work for me ...

[更新26-06-2015]

正如我在Android Bug来源中报告此bug所示,现在Bug被标记为未来版本,这是链接

As I have reported this bug at Android Bug source Now Bug is marked as a future release, here is the link https://code.google.com/p/android/issues/detail?id=176300

所以我们可以说该错误不再存在于将来的库中.因此,您不必使用棘手的解决方案.当将来的版本发行号是针对该错误的维护时,我还将再次更新此答案.

So we can say that the bug is no more exist on future library. So you don't have to use tricky solution. i will also update this answer again when Future version release number is maintion for this bug.

这篇关于如何以编程方式将子菜单项添加到新的材质设计android支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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