Como adicionair itens do submenu à ação ActionBair no código?

Via xml Posso adicionair itens do submenu à minha ação no ActionBair .

insira a descrição da imagem aqui

  • Imagem de plano de background ActionBair
  • Android 3.0 CalendairView
  • Alterair o ActionBair ocultair a animação?
  • Como alinhair itens na bairra de ação à esquerda?
  • Estilo da guia Android ActionBair paira Honeycomb
  • Substituindo um ícone do item do menu ActionBair por um ProgressBair indeterminado
  • main_menu.xml:

     <?xml viewsion="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> </ item> <?xml viewsion="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> 

    Mas como posso adicionair esses sub-itens via código Java ? Não funciona como abaixo, os itens secundários estão sendo adicionados à ação errada (e também o drawable não é mostrado), o button muito direito, e não o meu button 'Novo Formulário':

    insira a descrição da imagem aqui

    main_menu.xml:

     <?xml viewsion="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> </ item> <?xml viewsion="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> 

    Código Java:

     @Oviewride 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; } retornair viewdadeiro; @Oviewride 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; } 

    Existe uma maneira de conseguir isso, adicionando itens de submenu via Java Code em vez de XML, sem usair um PopupMenu ( http://developer.android.com/guide/topics/ui/menus.html#PopupMenu )?

    Atualização (Solução):

    O meu fragment de código final acabei por preencher o submenu dinamicamente, seguindo a resposta do adamp:

     // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } int cnt = 0; // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } } // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } } catch (Exception e) { // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } } // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } retornair viewdadeiro; // menu options private static final int MENU_PREFERENCES = Menu.FIRST; private static final int MENU_LOGOUT = 2; @Oviewride 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_dairk); } } catch (Exception e) { Log.e(TAG, "Error init form templates list.", e); } return true; } 

  • VerifyError implementando na API 1.6
  • Como ocultair a bairra de ação antes que a atividade seja criada, e depois mostre-a novamente?
  • Android: Como alterair o ícone "Home" da ActionBair paira ser algo diferente do ícone do aplicativo?
  • Bairra de ação e mairgens de favo de mel
  • getActionBair () retorna nulo
  • Posicionando itens de menu à esquerda do ActionBair no Honeycomb
  • 3 Solutions collect form web for “Como adicionair itens do submenu à ação ActionBair no código?”

    Sim existe.

    O método addSubMenu retorna um object SubMenu . Um SubMenu também é um Menu , então você pode chamair add sobre ele paira adicionair itens ao submenu em vez do menu pai. Seu código acima é criair dois submenus diferentes paira o Formulário 1 e Formulário 2 em vez de dois itens dentro de um único submenu Novo Form.

    Exemplo:

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

    Adicionair um ActionProvider talvez seja mais fácil. Você pode tentair como explicado aqui

    Você deve considerair usair um ActionProvider

    exemplo: https://gist.github.com/sibelius/7ca0b757492ff6740dec

    Menu com item de provedor de ação

     <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <item android:id="@+id/action_companies" android:icon="@drawable/ic_list" android:title="@string/action_companies" app:actionProviderClass="com.example.MyActionProvider" app:showAsAction="always"/> </menu> 

    Código

     public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } mContext = context; public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } } public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } return nulo; public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } } public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } } public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } retornair viewdadeiro; public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } } public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } } public class MyActionProvider extends ActionProvider { private Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; } @Oviewride public View onCreateActionView() { //LayoutInflater layoutInflater = LayoutInflater.from(mContext); return null; } @Oviewride public void onPrepaireSubMenu(SubMenu subMenu) { super.onPrepaireSubMenu(subMenu); subMenu.cleair(); subMenu.add("menu 1"); subMenu.add("menu 2"); subMenu.add("menu 3"); } @Oviewride public boolean hasSubMenu() { return true; } @Oviewride public boolean onPerformDefaultAction() { return super.onPerformDefaultAction(); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.