# A menu item carries the following attributes:
\ onMenuClickListener
#Submenus
# Context Menus
# Alternative Menus
Responding to XML-Based Menu Items
3.0之后可以直接在XML中指定Menu callback method:
# Pop-up Menus in 4.0
Then:
# XML Menu Tags
Checkable Behavior Tags
use the checkableBehavior tag to control checkable behavior at a group level:
use the checked tag to control checkable behavior at an item level:
Menu Icon Tag
Name: A string title
Menu item ID: An integer
Group ID: An integer representing which group this item should be part of
Sort order: An integer identifying the order of this menu item when it is displayed in the menu
# The sort-order attribute
# The sort-order attribute
Secondary: start at 0x30000, defined by the constant Menu.CATEGORY_SECONDARY. such as system menus, alternative menus, and container menus-have different order-number ranges.
System: start at 0x20000 and are defined by the constant Menu.CATEGORY_SYSTEM.
Alternative: starts at 0x40000.defined by the constant Menu.CATEGORY_ALTERNATIVE.They’re usually contributed by external applications that provide alternative ways to deal with the data that is under consideration.
Container: starting at 0x10000, defined by the constant Menu.CATEGORY_CONTAINER.
# Creating a Menu
3.0之前使用了菜单才会激活onCreateOpintionsMenu(), 3.0之后因为 action bar 总是在activity中显示,onCreateOpintionsMenu()总是在activity创建时调用.
3.0之前使用了菜单才会激活onCreateOpintionsMenu(), 3.0之后因为 action bar 总是在activity中显示,onCreateOpintionsMenu()总是在activity创建时调用.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// call the base class to include system menus
super.onCreateOptionsMenu(menu);
menu.add(0 // Group
,1 // item id
,0 // order
,"append"); // title
menu.add(0,2,1,"item2");
menu.add(0,3,2,"clear");
// It is important to return true to see the menu
return true;
}
public boolean onCreateOptionsMenu(Menu menu)
{
// call the base class to include system menus
super.onCreateOptionsMenu(menu);
menu.add(0 // Group
,1 // item id
,0 // order
,"append"); // title
menu.add(0,2,1,"item2");
menu.add(0,3,2,"clear");
// It is important to return true to see the menu
return true;
}
# Menu Group
removeGroup(id)
setGroupCheckable(id, checkable, exclusive)
setGroupEnabled(id, boolean enabled)
setGroupVisible(id,visible)
setGroupCheckable(id, checkable, exclusive)
setGroupEnabled(id, boolean enabled)
setGroupVisible(id,visible)
# Responding to Menu Items Through Listeners
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
..
// for items handled
return true;
// for the rest
return super.onOptionsItemSelected(item);
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
..
// for items handled
return true;
// for the rest
return super.onOptionsItemSelected(item);
}
}
\ onMenuClickListener
//
Step 1
public class MyResponse implements OnMenuClickListener
{
// some local variable to work on
//
// Some constructors
@override
boolean onMenuItemClick(MenuItem item)
{
// do your thing
return true;
}
}
// Step 2
MyResponse myResponse = new MyResponse( );
menuItem.setOnMenuItemClickListener(myResponse);
public class MyResponse implements OnMenuClickListener
{
// some local variable to work on
//
// Some constructors
@override
boolean onMenuItemClick(MenuItem item)
{
// do your thing
return true;
}
}
// Step 2
MyResponse myResponse = new MyResponse( );
menuItem.setOnMenuItemClickListener(myResponse);
Note: onMenuItemClickListener executes when click, even before onOpintionsItemSelected() method is called.
If onMenuItemClick() return ture, no other callbacks are executed.
# Other Menu Types
# Icon Menu
If onMenuItemClick() return ture, no other callbacks are executed.
# Other Menu Types
# Icon Menu
//
add a menu item and remember it so that you can use it
// subsequently to set the icon on it.
MenuItem item = menu.add( );
item.setIcon(R.drawable.balloons);
// subsequently to set the icon on it.
MenuItem item = menu.add( );
item.setIcon(R.drawable.balloons);
#Submenus
private
void addSubMenu(Menu menu)
{
// Secondary items are shown just like everything else
int base=Menu.FIRST + 100;
SubMenu sm = menu.addSubMenu(base,base+1,Menu.NONE,"submenu");
sm.add(base,base+2,base+2,"sub item1");
sm.add(base,base+3,base+3,"sub item2");
sm.add(base,base+4,base+4,"sub item3");
// submenu item icons are not supported
item1.setIcon(R.drawable.icon48x48_2);
// the following is ok however
sm.setIcon(R.drawable.icon48x48_1);
// This will result in runtime exception
// sm.addSubMenu("try this");
}
Note: you cannot add additional submenus to a submenu.
{
// Secondary items are shown just like everything else
int base=Menu.FIRST + 100;
SubMenu sm = menu.addSubMenu(base,base+1,Menu.NONE,"submenu");
sm.add(base,base+2,base+2,"sub item1");
sm.add(base,base+3,base+3,"sub item2");
sm.add(base,base+4,base+4,"sub item3");
// submenu item icons are not supported
item1.setIcon(R.drawable.icon48x48_2);
// the following is ok however
sm.setIcon(R.drawable.icon48x48_1);
// This will result in runtime exception
// sm.addSubMenu("try this");
}
# Context Menus
activity.onCreateContextMenu(); // Context menu is owned by a view, the method to populate context menus resides in the Activity class.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.textViewId);
registerForContextMenu(tv);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.textViewId);
registerForContextMenu(tv);
}
不是每个View都必须有Context Menus, 必须为需要的view注册一个context view: activity.registerForContextMenu(view)
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Sample Context Menu");
menu.add(200, 200, 200, "item1");
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Sample Context Menu");
menu.add(200, 200, 200, "item1");
}
responding to context menu clicks. onContextItemSelected() is also available on the Activity class.
@Override
public boolean onContextItemSelected(MenuItem item)
{
if (item.getitemId() = some-menu-item-id)
{
// handle this menu item
return true;
}
other exception processing
}
public boolean onContextItemSelected(MenuItem item)
{
if (item.getitemId() = some-menu-item-id)
{
// handle this menu item
return true;
}
other exception processing
}
??
# Loading Menus Through XML Files
# Loading Menus Through XML Files
\ Structure of an XML Menu Resource File
Inflating XML Menu Resource Files
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This group uses the default category. -->
<group android:id="@+id/menuGroup_Main">
<item android:id="@+id/menu_testPick"
android:orderInCategory="5"
android:title="Test Pick" />
<item android:id="@+id/menu_testGetContent"
android:orderInCategory="5"
android:title="Test Get Content" />
<item android:id="@+id/menu_clear"
android:orderInCategory="10"
android:title="clear" />
<item android:id="@+id/menu_dial"
android:orderInCategory="7"
android:title="dial" />
<item android:id="@+id/menu_test"
android:orderInCategory="4"
android:title="@+string/test" />
<item android:id="@+id/menu_show_browser"
android:orderInCategory="5"
android:title="show browser" />
</group>
</menu>
<!-- This group uses the default category. -->
<group android:id="@+id/menuGroup_Main">
<item android:id="@+id/menu_testPick"
android:orderInCategory="5"
android:title="Test Pick" />
<item android:id="@+id/menu_testGetContent"
android:orderInCategory="5"
android:title="Test Get Content" />
<item android:id="@+id/menu_clear"
android:orderInCategory="10"
android:title="clear" />
<item android:id="@+id/menu_dial"
android:orderInCategory="7"
android:title="dial" />
<item android:id="@+id/menu_test"
android:orderInCategory="4"
android:title="@+string/test" />
<item android:id="@+id/menu_show_browser"
android:orderInCategory="5"
android:title="show browser" />
</group>
</menu>
Inflating XML Menu Resource Files
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater(); // from activity
inflater.inflate(R.menu.my_menu, menu);
// It is important to return true to see the menu
return true;
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater(); // from activity
inflater.inflate(R.menu.my_menu, menu);
// It is important to return true to see the menu
return true;
}
Responding to XML-Based Menu Items
private
void onOptionsItemSelected (MenuItem item)
{
if (item.getItemId() == R.id.menu_clear) // xml生成的menu用 R.id.ooxx, 和前面code生成的 item id 不一样, item id是在menu.add()里第2个参数.
{
// do something
}
else if (item.getItemId() == R.id.menu_dial)
{
// do something
}
etc
}
{
if (item.getItemId() == R.id.menu_clear) // xml生成的menu用 R.id.ooxx, 和前面code生成的 item id 不一样, item id是在menu.add()里第2个参数.
{
// do something
}
else if (item.getItemId() == R.id.menu_dial)
{
// do something
}
etc
}
<
item
android:id
=" "
android:onClick ="a-method-name-in-your-activity"
</item >
android:onClick ="a-method-name-in-your-activity"
</item >
# Pop-up Menus in 4.0
<
menu
xmlns:android
="http://schemas.android.com/apk/res/android"
>
<!-- This group uses the default category. -->
< group android:id ="@+id/menuGroup_Popup" >
< item android:id ="@+id/popup_menu_1"
android:title ="Menu 1" />
< item android:id ="@+id/popup_menu_2"
android:title ="Menu 2" />
</ group >
</ menu >
<!-- This group uses the default category. -->
< group android:id ="@+id/menuGroup_Popup" >
< item android:id ="@+id/popup_menu_1"
android:title ="Menu 1" />
< item android:id ="@+id/popup_menu_2"
android:title ="Menu 2" />
</ group >
</ menu >
//
Other activity code goes here
// Invoke the following method to show a popup menu
private void showPopupMenu()
{
// Get hold of a view to anchor the popup
// getTextView() can be any method that returns a view
TextView tv = getTextView();
// instantiate a popup menu
// the var "this" stands for activity
PopupMenu popup = new PopupMenu( this, tv);
// the following code for 3.0 sdk
// popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
// Or in sdk 4.0
popup.inflate(R.menu.popup_menu);
popup.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
// some local method to log that item
// See the sample project to see how this method works
appendMenuItemText(item);
return true;
}
}
);
popup.show();
}
// Invoke the following method to show a popup menu
private void showPopupMenu()
{
// Get hold of a view to anchor the popup
// getTextView() can be any method that returns a view
TextView tv = getTextView();
// instantiate a popup menu
// the var "this" stands for activity
PopupMenu popup = new PopupMenu( this, tv);
// the following code for 3.0 sdk
// popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
// Or in sdk 4.0
popup.inflate(R.menu.popup_menu);
popup.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
// some local method to log that item
// See the sample project to see how this method works
appendMenuItemText(item);
return true;
}
}
);
popup.show();
}
# XML Menu Tags
Group Category Tag
<
group
android:id
="@+id/some_group_id "
android:menuCategory ="secondary" >
android:menuCategory ="secondary" >
Checkable Behavior Tags
use the checkableBehavior tag to control checkable behavior at a group level:
<
group
android:id
="@+id/noncheckable_group"
android:checkableBehavior ="none" >
android:checkableBehavior ="none" >
<
item
android:id
=".."
android:title =""
android:checked ="true" />
android:title =""
android:checked ="true" />
Tags to Simulate a Submenu
submenu is represented as a menu element under a menu item:
<
item
android:title
="All without group"
>
< menu >
< item >
</ menu >
</ item >
< menu >
< item >
</ menu >
</ item >
Menu Icon Tag
<
item
android:id
=".. "
android:icon ="@drawable/some-file" />
android:icon ="@drawable/some-file" />
Menu Enabling/Disabling Tag
<
item
android:id
=".. "
android:enabled ="true"
android:icon ="@drawable/some-file" />
android:enabled ="true"
android:icon ="@drawable/some-file" />
Menu Item Shortcuts
<
item
android:id
=" "
android:alphabeticShortcut ="a"
</item >
android:alphabeticShortcut ="a"
</item >
Menu Visibility
<
item
android:id
=" "
android:visible ="true"
</item >
android:visible ="true"
</item >