小编Mor*_*tur的帖子

如何以编程方式将子菜单项添加到新的材质设计android支持库

我正在尝试将新菜单项添加到新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>
Run Code Online (Sandbox Code Playgroud)

我有@ 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.
Run Code Online (Sandbox Code Playgroud)

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

android navigation-drawer material-design

38
推荐指数
5
解决办法
5万
查看次数