Android: Pop up Menu on Menu button pressed
I want to have a normal menu button pop up a second menu.
I don't want a listview of options to display开发者_如何学C, I want a second, standard menu to pop up when I press a button on the menu.
Using openOptionsMenu(); works when you use onCreateOptionsMenu
but it does not work when you use: onPrepareOptionsMenu
Since I want to change the options on the menu dynamically, I need to use onPrepareOptionsMenu.
Thanks
Use a menu group for any menu item that doesn't have an action itself but opens up a second menu. Here's an example of one I did.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:titleCondensed="Cats" android:title="Cats" android:id="@+id/cats">
<menu>
<group android:id="@+id/catsGroup">
<item android:titleCondensed="Lolz Cats" android:title="Lolz Cats" android:id="@+id/twod"></item>
<item android:titleCondensed="Ugly Cats" android:title="Ugly Cats" android:id="@+id/ref"></item>
<item android:titleCondensed="Dumb Cats" android:title="Dumb Cats" android:id="@+id/vr"></item>
</group>
</menu>
</item>
<item android:titleCondensed="Dogs" android:title="Dogs" android:id="@+id/dogs">
<menu>
<group android:id="@+id/dogsGroup">
<item android:titleCondensed="Awesome Dog" android:title="Awesome Dog" android:id="@+id/pan"></item>
<item android:titleCondensed="Under Dog" android:title="Under Dog" android:id="@+id/zoom"></item>
<item android:titleCondensed="Snoopy" android:title="Snoopy" android:id="@+id/contrast"></item>
<item android:titleCondensed="Scooby Doo" android:title="Scooby Doo" android:id="@+id/page"></item>
<item android:titleCondensed="Pluto" android:title="Pluto" android:id="@+id/rotate"></item>
</group>
</menu>
</item>
</menu>
The first menu items that will show are "Cats" and "Dogs". When you select on of these, their sub items will be shown. Hope this helps.
精彩评论