Create submenus programmatically for existing Menu

2020-06-12 06:22发布

问题:

I have created my parent menus in xml , now i don't know how to create submenus under those parents using code. That means , parents are coded in menu.xml and submenus will be loaded based on a dynamic code as and when the data is available.

When i tried to use menu.addSubMenu it is creating a new parent menu item.

回答1:

mymenu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:id="@+id/menubutton_submenuButton"
            android:showAsAction="always"/>
        <menu>
            <!-- empty submenu, filled programatically -->
        </menu>
    </item>
</menu>

Then later in code:

@Override
public boolean onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.mymenu, menu);

    MenuItem menuItem = menu.findItem(R.id.menubutton_submenuButton);
    SubMenu subMenu = menuItem.getSubMenu();
    // @todo: add submenu items programmatically here.

    return super.onCreateOptionsMenu(menu, inflater);
}


回答2:

okk.. you should try this..

private static final int FILE = 0;
    private static final int EDIT = 1;

    private static final int NEW_MENU_ITEM = Menu.FIRST;
    private static final int SAVE_MENU_ITEM = NEW_MENU_ITEM + 1;

    private static final int UNDO_MENU_ITEM = SAVE_MENU_ITEM + 1;
    private static final int REDO_MENU_ITEM = UNDO_MENU_ITEM + 1;


@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i("this is test ", "oncreate option menu is called");
//      menu.add("this is first menu");
//      menu.add("this is second menu");
//      
        SubMenu fileMenu = menu.addSubMenu("File");
        SubMenu editMenu = menu.addSubMenu("Edit");
        fileMenu.add(FILE, NEW_MENU_ITEM, 0, "new");
        fileMenu.add(FILE, SAVE_MENU_ITEM, 1, "save");
        editMenu.add(EDIT, UNDO_MENU_ITEM, 0, "undo");
        editMenu.add(EDIT, REDO_MENU_ITEM, 1, "redo");
        return super.onCreateOptionsMenu(menu);
    }


回答3:

I know this is a very very old thread but hopefully this will help others like me who had the same requirement.

menu/movies.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <group>
        <item
            android:id="@+id/action_filter"
            android:showAsAction="never"
            android:title="Filter">
            <menu>
                <group android:menuCategory="container">
                    <item
                        android:id="@+id/action_genre"
                        android:title="Genre">
                        <menu>
                            <group android:checkableBehavior="single" />
                        </menu>
                    </item>
                </group>
            </menu>
        </item>
    </group>
</menu>

then programmatically on your activity/fragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.movies, menu);
    MenuItem menuItem = menu.findItem(R.id.action_filter).getSubMenu().findItem(R.id.action_genre).getSubMenu().add(Menu.NONE, 1, Menu.NONE, "Action");
    MenuItem menuItem = menu.findItem(R.id.action_filter).getSubMenu().findItem(R.id.action_genre).getSubMenu().add(Menu.NONE, 2, Menu.NONE, "Comedy");
}

Hope this helps.



标签: android menu