我正在尝试将新的菜单项添加到新的 Material Design 抽屉面板中的子菜单中。
<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 设置,但现在想添加新的菜单项:
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 文件中的菜单项,但不显示新添加的菜单项。
有什么想法如何做到这一点吗?
[2016 年 3 月 20 日更新]错误已解决。所以无需担心。
[以下内容已过时。]
向 NavigationView 添加动态菜单目前是设计支持库上的错误. And 我已将其报告给 android bug 源跟踪。所以要等到bug修复了。但如果你想要临时解决方案你能行的。首先添加您的动态菜单..
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");
添加菜单后,只需编写以下代码..
MenuItem mi = m.getItem(m.size()-1);
mi.setTitle(mi.getTitle());
目前这是一个很棘手的解决方案。但为我工作...
[更新2015年6月26日]
正如我在 Android Bug 源中报告的这个 Bug,现在 Bug 被标记为未来版本, 链接在这里https://code.google.com/p/android/issues/detail?id=176300 https://code.google.com/p/android/issues/detail?id=176300
所以我们可以说这个错误在未来的库中不再存在。所以你不必使用棘手的解决方案。当未来版本版本号维护此错误时,我还将再次更新此答案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)