如何子菜单项添加到动作条行动code? [英] How to add submenu items to ActionBar action in code?

查看:125
本文介绍了如何子菜单项添加到动作条行动code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过XML,我可以添加子菜单项到我的工作中的动作条

main_menu.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID / menu_new_form
          机器人:图标=@可绘制/ ic_new_form
          机器人:标题=@字符串/ menu_new_form
          机器人:showAsAction =ifRoom | withText>
        <菜单>
            <项目机器人:ID =@ + ID / Form1的
                  机器人:图标=@可绘制/附件
                  机器人:标题=表1
                  机器人:的onClick =的onSort/>
            <项目机器人:ID =@ + ID /窗口2
                  机器人:图标=@可绘制/附件
                  机器人:标题=表2
                  机器人:的onClick =的onSort/>
        < /菜单>
    < /项目>
< /菜单>
 

但我怎么可以通过Java code添加这些子项?它不工作,下面的子项获取添加到了错误的动作(也被拉伸未显示),非常右键,不是我的新表按钮:

main_menu.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID / menu_new_form
          机器人:图标=@可绘制/ ic_new_form
          机器人:标题=@字符串/ menu_new_form
          机器人:showAsAction =ifRoom | withText>
    < /项目>
< /菜单>
 

Java的code:

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    super.onCreateOptionsMenu(菜单);
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_menu,菜单);

    Log.d(MainMenu的,,菜单title0:+ menu.getItem(0).getTitle());
    //返回新形式

    menu.addSubMenu(0,Menu.NONE,1,表1)的setIcon(R.drawable.attachment)。
    menu.addSubMenu(0,Menu.NONE,2,表2)的setIcon(R.drawable.attachment)。
    返回true;
}
 

有没有办法做到这一点,通过Java code,而不是XML添加子菜单项,没有使用的PopupMenu ( <一href="http://developer.android.com/guide/topics/ui/menus.html#PopupMenu">http://developer.android.com/guide/topics/ui/menus.html#PopupMenu)?

更新(解决方案):

我的最后code段我结束了动态填充子菜单,下面adamp的答复:

  //菜单选项
私有静态最终诠释MENU_ preFERENCES = Menu.FIRST;
私有静态最终诠释MENU_LOGOUT = 2;

@覆盖
公共布尔onCreateOptionsMenu(最后菜单菜单){
    super.onCreateOptionsMenu(菜单);

    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_menu,菜单);
    menu.add(0,MENU_ preFERENCES,0,的getString(R.string.general_ preferences))。的setIcon(
            android.R.drawable.ic_menu_ preferences);

    //加载所有可用的表单模板
    光标C = managedQuery(FormsProviderAPI.FormsColumns.CONTENT_URI,NULL,NULL,NULL,NULL);
    尝试 {
        INT ixDisplayName = c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME);
        INT ixId = c.getColumnIndex(FormsProviderAPI.FormsColumns._ID);
        INT CNT = 0;
        而(c.moveToNext()){
            CNT ++;
            Log.d(ID:,ID:+ c.getInt(ixId)); //滥用组ID的格式ID
            menu.getItem(1).getSubMenu()addSubMenu(c.getInt(ixId),Menu.NONE,CNT,c.getString(ixDisplayName))的setIcon(R.drawable.attachment_dark)。;
        }
    }赶上(例外五){
        Log.e(TAG,错误初始化表单模板列表中。,E);
    }

    返回true;
}
 

解决方案

是的,有。

addSubMenu 方法返回一个 子菜单 对象。 A 子菜单也是一个 菜单 ,这样你就可以调用添加就可以将项目添加到子菜单,而不是上一级菜单。您的code以上是创建两个不同的子菜单,表格1及表2,而不是两个项目中的一个新形式的子菜单。

例如:

 子菜单的子菜单= menu.addSubMenu(0,Menu.NONE,1,新表)的setIcon(R.drawable.ic_new_form)。
submenu.add(表1)的setIcon(R.drawable.attachment)。
 

Via xml I can add submenu items to my action in the ActionBar.

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_new_form"
          android:icon="@drawable/ic_new_form"
          android:title="@string/menu_new_form"
          android:showAsAction="ifRoom|withText">
        <menu>
            <item android:id="@+id/form1"
                  android:icon="@drawable/attachment"
                  android:title="Form 1"
                  android:onClick="onSort" />
            <item android:id="@+id/form2"
                  android:icon="@drawable/attachment"
                  android:title="Form 2"
                  android:onClick="onSort" />
        </menu>
    </item>
</menu>

But how can I add these sub items via Java code? It doesn't work as below, the sub items are getting added to the wrong action (and also the drawable isn't shown), the very right button, not my 'New Form' button:

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_new_form"
          android:icon="@drawable/ic_new_form"
          android:title="@string/menu_new_form"
          android:showAsAction="ifRoom|withText">
    </item>
</menu>

Java Code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    Log.d("MainMenu", ",menu title0: " + menu.getItem(0).getTitle()); 
    // returns "New Form"

    menu.addSubMenu(0, Menu.NONE, 1, "Form 1").setIcon(R.drawable.attachment);
    menu.addSubMenu(0, Menu.NONE, 2, "Form 2").setIcon(R.drawable.attachment);
    return true;
}

Is there a way to achieve this, adding sub menu items via Java Code instead of XML, without using a PopupMenu (http://developer.android.com/guide/topics/ui/menus.html#PopupMenu)?

Update (Solution):

My final code snippet I ended up with to populate the submenu dynamically, following adamp's reply:

// menu options
private static final int MENU_PREFERENCES = Menu.FIRST;
private static final int MENU_LOGOUT = 2;

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    menu.add(0, MENU_PREFERENCES, 0, getString(R.string.general_preferences)).setIcon(
            android.R.drawable.ic_menu_preferences);

    // load all available form templates
    Cursor c = managedQuery(FormsProviderAPI.FormsColumns.CONTENT_URI, null, null, null, null);
    try {
        int ixDisplayName = c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME);
        int ixId = c.getColumnIndex(FormsProviderAPI.FormsColumns._ID);
        int cnt = 0;
        while (c.moveToNext()) {
            cnt++;
            Log.d("ID: ", "ID: "+ c.getInt(ixId));  // misusing the group id for the form id
            menu.getItem(1).getSubMenu().addSubMenu(c.getInt(ixId), Menu.NONE, cnt, c.getString(ixDisplayName)).setIcon(R.drawable.attachment_dark);
        }
    } catch (Exception e) {
        Log.e(TAG, "Error init form templates list.", e);
    }

    return true;
}

解决方案

Yes, there is.

The addSubMenu method returns a SubMenu object. A SubMenu is also a Menu, so you can call add on it to add items to the submenu rather than the parent menu. Your code above is creating two different submenus for Form 1 and Form 2 rather than two items within a single New Form submenu.

Example:

SubMenu submenu = menu.addSubMenu(0, Menu.NONE, 1, "New Form").setIcon(R.drawable.ic_new_form);
submenu.add("Form 1").setIcon(R.drawable.attachment);

这篇关于如何子菜单项添加到动作条行动code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆