Menus

Menus

Menus are a common user interface component in many types of applications.

There are three fundamental types of menus or action presentations on all versions of Android:

a> Options menu and action bar

The options menu is the primary collection of menu items for an activity.

b> Context menu and contextual action mode

A context menu is a floating menu that appears when the user performs a long-click on an element.

c> Popup menu

A popup menu displays a list of items in a vertical list that's anchored to the view that invoked the menu.l

Defining a Menu in XML

Instead of building a menu in your activity's code, you should define a menu and all its items in an XML menu resource.

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. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.

<item>

Creates a menu item, which represents a single item in a menu. This element may contain a nested <menu> element in order to create a submenu.

<group>

An option, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility.

To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using MenuInflater.inflate().

Creating an Options Menu

The options menu is where you should include actions and other options that are relevant to the current activity context.

To specify the options menu for an activity, override onCreateOptionsMenu(). In this method, you can inflate your menu resource (defined in XML) into the menu provided in the callback.

Handling click events

When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's onOptionsItemSelected() method.

When you successfully handle a menu item, return true. If you don't handle the menu item, you should call the superclass implementation of onOptionsItemSelected().

Changing menu items at runtime

After the system calls onCreateOptionsMenu(), it retains an instance of the menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.

If you want to modify the options menu base on events that occur during the activity lifecycle, you cal also do so in the onPrepareOptionsMenu() method. This method passes you the menu object as it currently exists so you can modify it, such as add, remove, or disable items.

On Android 3.0 and higher, when an event occurs and you want to perform a menu update, you must call invalidateOptionMenu() to request that the system call onPrepareOptionsMenu().

Creating Contextual Menus

A contextual menu offers actions that affect a specific item or context frame in the UI.

There are two ways to provide contextual actions: In a floating context menu, and In the contextual action mode.

Creating a floating context menu

To provide 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 list view or grid view and you want each item to provide the same context menu, register all items for a context menu by passing the list view or grid view 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.

3> Implement onContextItemSelected().

Using the contextual action mode

When a user enables this mode by selecting an item, a contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected item(s).

The contextual action bar is not necessarily associated with the action bar.

For views that provide contextual actions, you should usually invoke the contextual action mode upon one of two events (or both):

a> The user performs a long-click on the view.

b> The user selects a checkbox or similar UI component within the view.

How your application invokes the contextual action mode and defines the behavior for each action depends on your design. There are basically two designs:

a> For contextual actions on individual, arbitrary views.

b> For batch contextual actions on groups of items in a list view or grid view (allowing the user to select multiple items and perform an action on them all).

Enabling the contextual action mode for individual views

If you want to invoke the contextual action mode only when the user selects specific views, you should:

1> Implement the ActionMode.Callback interface.

2> Call startActionMode() when you want to show the bar.

Enabling batch contextual actions in a list view or grid view

If you have a collection of items in a list view or grid view (or another extension of AbsListView) and want to allow user to perform batch actions, you should:

1> Implement the AbsListView.MultiChoiceModeListener interface and set it for the view group with setMultiChoiceModeListener().

2> Call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument.

When the user selects an item with a long-click, the system calls the onCreateActionMode() and display the contextual action bar with the specified actions.

In some cases in which the contextual actions provide common action items, you might want to add a checkbox or a similar UI element that allows users to select items, because they might not discover the long-click behavior. When a user selects the checkbox, you can invoke the contextual action mode by setting the respective list item to the checked state with setItemChecked().

Creating a Popup Menu

A PopupMenu is a modal menu anchored to a View. It appears below the anchor view if there is room, or above the view otherwise.It's useful for:

a> Providing an overflow-style menu for actions that relate to specific content.

b>Providing a second part of a command sentence

c> Providing a drop-down similar to Spinner that does not retain a persistent selection.

If you define your menu in  XML, here's how you can show the popup menu:

1> Instantate a PopupMenu with its constructor, which takes the current application context and the view to which the menu should be anchored.

2> Use MenuInflater to inflate your menu resource into the menu object returned by PopupMenu.getMenu().

3> Call PopupMenu.show().

The menu is dismissed when the user selects an item or touches outside the menu area.

Handling click events

To perform an action when the user selects a menu item, you must implement the PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling setOnMenuItemclickListener().

Creating Menu Groups

A menu group is a collection of menu items that share certain traits. With a group, you can:

a> Show or hide all items with setGroupVisible().

b> Enable or disable all items with setGroupEnabled().

c> Specify whether all items are checkable with setGroupCheckable().

Using checkable menu items

You can define the checkable behavior for individual menu items using the android:checkable attribute in the <item> element, or for an entire group with the android:checkableBehavior attribute in the <group> element.

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()).

Adding Menu items Based on an Intent

Allowing your activity to be added to other menus

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值