menu

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.





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值