Android向menuItem添加子菜单,addSubMenu()在哪里?

发布于 2024-11-29 07:48:56 字数 500 浏览 3 评论 0原文

我想根据我的参数以编程方式将 OptionsMenu 内的子菜单添加到 menuItem 中。我检查了android sdk中的“MenuItem”,没有addSubMenu()方法!尽管你可以找到“hasSubMenu()”和“getSubMenu”。

正在考虑在 onCreateOptionsMenu 中执行此操作:

public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem mi = menu.getItem(MYITEMID);  // << this is defined in my XML optionsMenu
    SubMenu subm = mi.addSubMenu(0,1,0,"Map 1"); // no addSubMenu() method!!!???
....

How do I create a submenu inside a menuitem in code?

I want to add a submenu inside my OptionsMenu to a menuItem, programatically according to my parameters. I've checked "MenuItem" in android sdk and there is no addSubMenu() method!, although you can find "hasSubMenu()" and "getSubMenu".

Was thinking on doing this in onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem mi = menu.getItem(MYITEMID);  // << this is defined in my XML optionsMenu
    SubMenu subm = mi.addSubMenu(0,1,0,"Map 1"); // no addSubMenu() method!!!???
....

How do I create a submenu inside a menuitem in code?

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

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

发布评论

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

评论(8

弄潮 2024-12-06 07:48:56

有时 Android 的怪异确实令人惊奇(而且有趣......)。我这样解决了这个问题:

a)在 XML 中定义一个子菜单占位符,如下所示:

<item android:visible="true" android:id="@+id/m_area"
   android:titleCondensed="Areas"
   android:title="Areas"
   android:icon="@drawable/restaur"
   android:enabled="true"> 
   <menu>
    <item android:id="@+id/item1" android:title="Placeholder"></item>
   </menu>
</item>

b)在 OnCreateOptionsMenu 中获取子菜单项,清除它并添加我的子菜单项,如下所示:

    public boolean onCreateOptionsMenu(Menu menu) { 
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.mapoptions, menu);

            int idx=0;
            SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
            subm.clear(); // delete place holder

            while(true)
            {
                anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
                if(anarea == null)
                    break;
                subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
                ++idx;
            }
}

Sometimes Android weirdness is really amazing (and amusing..). I solved it this way:

a) Define in XML a submenu placeholder like this:

<item android:visible="true" android:id="@+id/m_area"
   android:titleCondensed="Areas"
   android:title="Areas"
   android:icon="@drawable/restaur"
   android:enabled="true"> 
   <menu>
    <item android:id="@+id/item1" android:title="Placeholder"></item>
   </menu>
</item>

b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this:

    public boolean onCreateOptionsMenu(Menu menu) { 
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.mapoptions, menu);

            int idx=0;
            SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
            subm.clear(); // delete place holder

            while(true)
            {
                anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
                if(anarea == null)
                    break;
                subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
                ++idx;
            }
}
奶气 2024-12-06 07:48:56

我知道这是一个老问题,但我自己也遇到过这个问题。
最直接的方法似乎是简单地将项目本身指定为子菜单,然后添加到该项目。
例如:

menu.add(groupId, MENU_VIEW, Menu.NONE, getText(R.string.menu_view));
menu.add(groupId, MENU_EDIT, Menu.NONE, getText(R.string.menu_edit));
SubMenu sub=menu.addSubMenu(groupId, MENU_SORT, Menu.NONE, getText(R.string.menu_sort));
sub.add(groupId, MENU_SORT_BY_NAME, Menu.NONE, getText(R.string.menu_sort_by_name));
sub.add(groupId, MENU_SORT_BY_ADDRESS, Menu.NONE, getText(R.string.menu_sort_by_address));
:
:

I know this is an old question, but I just came across this problem myself.
The most straightforward way of doing this, seems to be to simply specify the item itself as a submenu, then add to this item.
E.g.:

menu.add(groupId, MENU_VIEW, Menu.NONE, getText(R.string.menu_view));
menu.add(groupId, MENU_EDIT, Menu.NONE, getText(R.string.menu_edit));
SubMenu sub=menu.addSubMenu(groupId, MENU_SORT, Menu.NONE, getText(R.string.menu_sort));
sub.add(groupId, MENU_SORT_BY_NAME, Menu.NONE, getText(R.string.menu_sort_by_name));
sub.add(groupId, MENU_SORT_BY_ADDRESS, Menu.NONE, getText(R.string.menu_sort_by_address));
:
:
梦太阳 2024-12-06 07:48:56

这是一个完整的答案,它基于使用占位符的想法,但主要使用 xml 来添加子菜单。

如果您有一个类似 main_menu.xml 的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="My Menu"
    android:id="@+id/my_menu_item">
    <!-- A empty SubMenu -->
    <menu></menu>
</item>
</menu>

创建另一个菜单 sub_menu.xml ,它将在 my_menu_item 中使用:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:title="SubMenu One"
    android:id="@+id/submenu_one" />
  <item android:title="SubMenu Two"
    android:id="@+id/submenu_two" />
  <item android:title="SubMenu Three"
    android:id="@+id/submenu_three" />
</menu>

在您的 onCreateOptionsMenu 中:

public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate your main_menu into the menu
   getMenuInflater().inflate(R.menu.main_menu, menu);

   // Find the menuItem to add your SubMenu
   MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);

   // Inflating the sub_menu menu this way, will add its menu items 
   // to the empty SubMenu you created in the xml
   getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());

}

这个解决方案很好,因为充气器处理了大部分工作。

Here's a complete answer which builds on the idea of using a placeholder but uses mostly xml to add the submenu.

If you have a menu like so called main_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="My Menu"
    android:id="@+id/my_menu_item">
    <!-- A empty SubMenu -->
    <menu></menu>
</item>
</menu>

Create another menu sub_menu.xml which will be used in my_menu_item:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:title="SubMenu One"
    android:id="@+id/submenu_one" />
  <item android:title="SubMenu Two"
    android:id="@+id/submenu_two" />
  <item android:title="SubMenu Three"
    android:id="@+id/submenu_three" />
</menu>

In your onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate your main_menu into the menu
   getMenuInflater().inflate(R.menu.main_menu, menu);

   // Find the menuItem to add your SubMenu
   MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);

   // Inflating the sub_menu menu this way, will add its menu items 
   // to the empty SubMenu you created in the xml
   getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());

}

This solution is nice since the inflater handles most of the work.

溺渁∝ 2024-12-06 07:48:56

执行此操作的最佳方法是在 xml 菜单文件中。您可以通过在 item 内创建一个新的 menu 对象来实现此目的:

<menu>
  <item>
    ...
    <menu>
      ...
    </menu>
    ...
  </item>
</menu>

The best way to do this is in your xml menu file. You can do this by creating a new menu object inside of an item:

<menu>
  <item>
    ...
    <menu>
      ...
    </menu>
    ...
  </item>
</menu>
携君以终年 2024-12-06 07:48:56

为了提供 Phil 答案的综合示例,这里是我完整的工作 XML,该菜单包含两个选项,每个菜单都是一个包含三个选项的菜单。我打算在顶层添加第三个菜单......

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:HTMLCode="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/Examine"
        android:title="@string/Examine"
        HTMLCode:showAsAction="always">

        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
            <item android:id="@+id/load"
                android:title="@string/load"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/findfirst"
                android:title="@string/findfirst"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/findnext"
                android:title="@string/FindNext"
                HTMLCode:showAsAction="ifRoom|withText" />
        </menu>
    </item>

    <item android:id="@+id/Redirect"
        android:title="@string/Redirect"
        HTMLCode:showAsAction="ifRoom|withText">

        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
            <item android:id="@+id/getRedirect"
                android:title="@string/getRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/toggleRedirect"
                android:title="@string/toggleRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/copyRedirect"
                android:title="@string/copyRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />
        </menu>
    </item>
</menu>

To provide a comprehensive example of Phil's answer, here is my complete, working XML for a menu with two choices, each of which is a menu with three choices. I intend to add a third menu to the top level ...

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:HTMLCode="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/Examine"
        android:title="@string/Examine"
        HTMLCode:showAsAction="always">

        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
            <item android:id="@+id/load"
                android:title="@string/load"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/findfirst"
                android:title="@string/findfirst"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/findnext"
                android:title="@string/FindNext"
                HTMLCode:showAsAction="ifRoom|withText" />
        </menu>
    </item>

    <item android:id="@+id/Redirect"
        android:title="@string/Redirect"
        HTMLCode:showAsAction="ifRoom|withText">

        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
            <item android:id="@+id/getRedirect"
                android:title="@string/getRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/toggleRedirect"
                android:title="@string/toggleRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />

            <item android:id="@+id/copyRedirect"
                android:title="@string/copyRedirect"
                HTMLCode:showAsAction="ifRoom|withText" />
        </menu>
    </item>
</menu>
隔纱相望 2024-12-06 07:48:56

您应该考虑使用 ActionProvider。

public class MyActionProvider extends ActionProvider {

    private Context mContext;

    public MyActionProvider(Context context) {
        super(context);

        mContext = context;
    }

    @Override
    public View onCreateActionView() {
        //LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        return null;
    }

    @Override
    public void onPrepareSubMenu(SubMenu subMenu) {
        super.onPrepareSubMenu(subMenu);

        subMenu.clear();

        subMenu.add("menu 1");
        subMenu.add("menu 2");
        subMenu.add("menu 3");
    }

    @Override
    public boolean hasSubMenu() {
        return true;
    }

    @Override
    public boolean onPerformDefaultAction() {
        return super.onPerformDefaultAction();
    }
}

You should consider use a ActionProvider instead.

public class MyActionProvider extends ActionProvider {

    private Context mContext;

    public MyActionProvider(Context context) {
        super(context);

        mContext = context;
    }

    @Override
    public View onCreateActionView() {
        //LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        return null;
    }

    @Override
    public void onPrepareSubMenu(SubMenu subMenu) {
        super.onPrepareSubMenu(subMenu);

        subMenu.clear();

        subMenu.add("menu 1");
        subMenu.add("menu 2");
        subMenu.add("menu 3");
    }

    @Override
    public boolean hasSubMenu() {
        return true;
    }

    @Override
    public boolean onPerformDefaultAction() {
        return super.onPerformDefaultAction();
    }
}
优雅的叶子 2024-12-06 07:48:56

我只是在 xml 文件中创建子菜单,并在运行时从菜单对象获取子菜单(使用 findItem(id) 方法)并使用 submenu.setVisible(boolean) 在运行时添加/删除它。

I would just create the submenu in xml file, and in run time get the submenu from menu object, (using findItem(id) method) and use submenu.setVisible(boolean) to add/remove it on run time.

最初的梦 2024-12-06 07:48:56
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu1" android:alphabeticShortcut="a"
    android:title="Menu No. 1" android:orderInCategory="1" />
<item android:id="@+id/menu2" android:alphabeticShortcut="b"
    android:title="Menu No. 2" android:orderInCategory="2">
    <menu >
    <group android:id="@+id/group2" android:checkableBehavior="single">
        <item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
        <item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
    </group>   
    </menu>
</item>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu1" android:alphabeticShortcut="a"
    android:title="Menu No. 1" android:orderInCategory="1" />
<item android:id="@+id/menu2" android:alphabeticShortcut="b"
    android:title="Menu No. 2" android:orderInCategory="2">
    <menu >
    <group android:id="@+id/group2" android:checkableBehavior="single">
        <item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
        <item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
    </group>   
    </menu>
</item>

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