Android 菜单和子菜单
我在创建单选按钮子菜单时遇到问题,当选择用户按下菜单按钮时出现的选项菜单中的菜单项时,会显示该单选按钮子菜单。这是我到目前为止所得到的:
// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch item.getItemId() {
case R.id.expandRadioMenu:
// Show the sub-menu and collapse the initial menu
return true;
}
}
res/menu/menu.xml
的 XML 如下:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:visible="true"
android:enabled="true"
android:checkable="false"
android:icon="@drawable/ic_menu_mark"
android:id="@+id/expandRadioMenu"
android:title="Select"
android:titleCondensed="select">
<menu>
<group
android:enabled="true"
android:visible="false"
android:checkableBehavior="single"
android:id="@+id/radio">
<item
android:enabled="true"
android:visible="true"
android:title="Foo"
android:titleCondensed="Foo"
android:id="@+id/foo">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="Bar"
android:titleCondensed="Bar"
android:id="@+id/bar">
</item>
</group>
</menu>
</item>
</menu>
I'm having trouble creating a radio-button sub-menu that is shown when a menu item from the options menu that appears when a user presses the menu button is selected. This is what I have so far:
// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch item.getItemId() {
case R.id.expandRadioMenu:
// Show the sub-menu and collapse the initial menu
return true;
}
}
The XML of res/menu/menu.xml
is as follows:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:visible="true"
android:enabled="true"
android:checkable="false"
android:icon="@drawable/ic_menu_mark"
android:id="@+id/expandRadioMenu"
android:title="Select"
android:titleCondensed="select">
<menu>
<group
android:enabled="true"
android:visible="false"
android:checkableBehavior="single"
android:id="@+id/radio">
<item
android:enabled="true"
android:visible="true"
android:title="Foo"
android:titleCondensed="Foo"
android:id="@+id/foo">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="Bar"
android:titleCondensed="Bar"
android:id="@+id/bar">
</item>
</group>
</menu>
</item>
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道它不是特别吸引人,但是您是否尝试过将子菜单移动到单独的 .xml 文件中?我以前见过它的工作原理,但这不是一个非常有希望的解决方案。我想它可能会进一步确定问题所在。
I know it's not particularly appealing, but have you tried moving the sub menu into a separate .xml file? I've seen it work before, but it's not a terribly hopeful solution. I suppose it might further identify the problem though.