以编程方式为从 XML 创建的现有菜单创建子菜单

发布于 2024-11-16 14:10:04 字数 141 浏览 4 评论 0原文

我已经在 xml 中创建了父菜单,现在我不知道如何使用代码在这些父菜单下创建子菜单。这意味着,父菜单在 menu.xml 中进行编码,子菜单将在数据可用时基于动态代码加载。

当我尝试使用 menu.addSubMenu 时,它正在创建一个新的父菜单项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨巷深深 2024-11-23 14:10:04

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>

然后在代码中:

@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);
}

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);
}
眼泪淡了忧伤 2024-11-23 14:10:04

好吧..你应该试试这个..

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);
    }

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);
    }
庆幸我还是我 2024-11-23 14:10:04

我知道这是一个非常非常古老的线程,但希望这能帮助像我这样有相同要求的其他人。

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>

然后以编程方式在您的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");
}

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文