Przez xml Mogę dodać elementy podmenu do mojej akcji w ActionBar
.Jak dodać elementy podmenu do akcji ActionBar w kodzie?
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>
Ale jak mogę dodać te elementy podrzędne pośrednictwem kodu Java? To nie działa jak poniżej, podrzędne pozycje są coraz dodany do niewłaściwego działania (a także odkształcalne nie pokazano), bardzo prawym przyciskiem, a nie mój przycisk „Nowa forma”:
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>
kod Java:
@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;
}
Czy istnieje sposób, aby to osiągnąć, dodając podrzędne pozycje menu za pomocą kodu Java zamiast XML, bez przy użyciu PopupMenu
(http://developer.android.com/guide/topics/ui/menus.html#PopupMenu)?
Update (Solution):
Mój końcowy fragment kodu skończyło się zapełnić podmenu dynamicznie, po odpowiedzi adamp za:
// 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;
}
Używam tego kod do mojej aplikacji @Override \t public boolean onCreateOptionsMenu (MENU) { \t \t // TODO Auto-generowane metoda skrótowej \t \t Super.onCreateOptionsMenu (menu); \t \t MenuInflater inflater = getMenuInflater(); \t \t inflater.inflate (R.menu.poi_menu, menu); \t \t do (int i = 0; i