如何在代码中向 ActionBar 操作添加子菜单项?
通过 xml,我可以将子菜单项添加到 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>
但是如何通过 Java 代码添加这些子项目 ?它不起作用,如下所示,子项目被添加到错误的操作(并且未显示可绘制对象),非常正确的按钮,而不是我的“新表单”按钮:
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 代码:
@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;
}
有没有办法实现此目的,通过 Java 代码而不是 XML 添加子菜单项,没有 使用PopupMenu
(http://developer.android. com/guide/topics/ui/menus.html#PopupMenu)?
更新(解决方案):
我最终得到的代码片段是根据 adamp 的回复动态填充子菜单的:
// 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,有。
addSubMenu
方法返回一个SubMenu
对象。SubMenu
也是一个Menu
,因此您可以对其调用add
将项目添加到子菜单而不是父菜单。上面的代码为 Form 1 和 Form 2 创建两个不同的子菜单,而不是在单个 New Form 子菜单中创建两个项目。例子:
Yes, there is.
The
addSubMenu
method returns aSubMenu
object. ASubMenu
is also aMenu
, so you can calladd
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:
添加一个 ActionProvider 也许会更容易。您可以按照此处的说明进行尝试
Adding an ActionProvider maybe it's easier. You can try as explained here
您应该考虑使用 ActionProvider
示例:https://gist.github.com/sibelius/7ca0b757492ff6740dec
菜单
带有操作提供程序项目代码的
You should consider to use an ActionProvider
example: https://gist.github.com/sibelius/7ca0b757492ff6740dec
Menu with action provider item
Code