Options menu and action bar
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with
Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other
options.
Context menu and contextual action mode
Popup menu
For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity's code, you should define a menu and all its
items in an XML menu resource. You can then inflate the menu resource (load it as a Menu object) in your activity or fragment.
To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements:
<menu>:Defines a Menu, which is a container for menu items.
<item>:Creates a MenuItem, which represents a single item in a menu.
<group>:An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility
The <item> element supports several attributes you can use to define an item's appearance and behavior. The items in the above menu include the following attributes:
android:id
android:icon
android:title
android:showAsAction:Specifies when and how this item should appear as an action item in the action bar.
Creating Contextual Menus(上下文菜单)
You can provide a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in which the user can perform direct
actions on each item.
There are two ways to provide contextual actions:
1 . In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that
declares support for a context menu. Users can perform a contextual action on one item at a time.
2 . In the contextual action mode. This mode is a system implementation of ActionMode that displays a contextual action bar at the top of the screen with action items that
affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it).
Creating a floating context menu
1,Register the View to which the context menu should be associated by calling registerForContextMenu() and pass it the View.
If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or
GridView to registerForContextMenu().
2.Implement the onCreateContextMenu() method in your Activity or Fragment.
When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. This is where you define the menu items, usually by inflating a menu resource. For example:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
3.Implement onContextItemSelected().
Using the contextual action mode
一般这种方式,点击后menu出现在顶部,有点像actionbar
Creating a Popup Menu
Note: PopupMenu is available with API level 11 and higher.