How to programmatically add a submenu item to the new material design android support library

前端 未结 5 1574
时光说笑
时光说笑 2020-11-28 23:26

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



        
5条回答
  •  渐次进展
    2020-11-29 00:13

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

    [This below contetnt is outdated.]

    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 ...

    [update 26-06-2015]

    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.

提交回复
热议问题