【0】先看一段官方的说明:

Menus are an important part of an activity's user interface, which provide users a familiar way to perform actions. Android offers a simple framework for you to add standard menus to your application.

There are three types of application menus:

Options Menu
The primary collection of menu items for an activity, which appears when the user touches the MENU button. When your application is running on Android 3.0 or later, you can provide quick access to select menu items by placing them directly in the Action Bar, as "action items."
这个其实就像是这样的菜单:
Context Menu
A floating list of menu items that appears when the user touches and holds a view that's registered to provide a context menu.
这个的话其实可以理解成PC上点击右键的菜单:

Submenu
A floating list of menu items that appears when the user touches a menu item that contains a nested menu.
这个Submenu就是子菜单的意思,点击一个菜单后进入子菜单,有点像 PC application's menu bar (File, Edit, View, etc.).

【1】使用XML文件来创建OptionsMenu

  • 这是使用inflater XML的方式来创建一个Menu.显然我们需要在res/menu/的目录下创建类似下面的一个文件:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/new_game"          android:icon="@drawable/ic_new_game"          android:title="@string/new_game" />    <item android:id="@+id/help"          android:icon="@drawable/ic_help"          android:title="@string/help" /></menu>

android:id
A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it.
android:icon
A reference to a drawable to use as the item's icon.
android:title
A reference to a string to use as the item's title.除了上面三个属性外,更多的我们可以参考 Menu Resource

  • Inflating a Menu Resource
使用MenuInflater来创建一个Menu
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.game_menu, menu);    return true;}
   在3.0之前会默认显示6个items,如果超出6个,最后一个就用more来显示,点击more会显示其余的items.              3.0之前   onCreateOptionsMenu是在第一次点击menu的时候被呼叫到,而3.0之后是在activity被创建的时候就被呼叫了      
当点击menu按键的时候会call 到onCreateOptionsMenu.
  • 当用户点击其中的item的适合会call到下面的方法:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    // Handle item selection    switch (item.getItemId()) {    case R.id.new_game:        newGame();        return true;    case R.id.help:        showHelp();        return true;    default:        return super.onOptionsItemSelected(item);    }}

  • 根据AP运行情况来调整Menu的Item
onCreateOptionsMenu()的方法只会call到一次,之后一直存在直到activity销毁,如果我们需要在程序运行时根据不同的情况来调整menu的item话,那么需要使用onPrepareOptionsMenu()这个方法传入一个已经存在的Menu,之后我们可以进行remove, add, disable, or enable menu items.

On Android 2.3 and lower, the system callsonPrepareOptionsMenu()each time the user opens the Options Menu. On Android 3.0 and higher, you must callinvalidateOptionsMenu()when you want to update the menu, because the menu is always open. The system will then callonPrepareOptionsMenu()so you can update the menu items.


【2】创建Context Menu

On Android, a context menu is displayed when the user performs a "long press" (press and hold) on an item.
我们需要使用长时间点击某个Item来显示一个Context Menu.通常使用在ListView中比较多。 需要注意的是,我们使用Context Menu之前需要进行注册的动作,如下描述: In order for a View to provide a context menu, you must "register" the view for a context menu. CallregisterForContextMenu()and pass it theViewyou want to give a context menu. When this View then receives a long-press, it displays a context menu.

For example: Register a ListView If your activity uses aListViewand you want all list items to provide a context menu, register all items for a context menu by passing theListViewtoregisterForContextMenu(). For example, if you're using aListActivity, register all list items like this: registerForContextMenu(getListView());


实现创建Context Menu,我们需要用到onCreateContextMenu()andonContextItemSelected().
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,                                ContextMenuInfo menuInfo) {  super.onCreateContextMenu(menu, v, menuInfo);  MenuInflater inflater = getMenuInflater();  inflater.inflate(R.menu.context_menu, menu);}

@Overridepublic boolean onContextItemSelected(MenuItem item) {  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  switch (item.getItemId()) {  case R.id.edit:    editNote(info.id);    return true;  case R.id.delete:    deleteNote(info.id);    return true;  default:    return super.onContextItemSelected(item);  }}

Note:Items in a context menu do not support icons or shortcut keys.

【3】Creating Submenus
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/file"          android:icon="@drawable/file"          android:title="@string/file" >        <!-- "file" submenu -->        <menu>            <item android:id="@+id/create_new"                  android:title="@string/create_new" />            <item android:id="@+id/open"                  android:title="@string/open" />        </menu>    </item></menu>

When the user selects an item from a submenu, the parent menu's respective on-item-selected callback method receives the event. For instance, if the above menu is applied as an Options Menu, then theonOptionsItemSelected()method is called when a submenu item is selected.

【4】Notes

  • Menu groups

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

  • Show or hide all items withsetGroupVisible()
  • Enable or disable all items withsetGroupEnabled()
  • Specify whether all items are checkable withsetGroupCheckable()

You can create a group by nesting<item>elements inside a<group>element in your menu resource or by specifying a group ID with the theadd()method.

下面是一个MenuGroups的例子:
   
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/item1"          android:icon="@drawable/item1"          android:title="@string/item1" />    <!-- menu group -->    <group android:id="@+id/group1">        <item android:id="@+id/groupItem1"              android:title="@string/groupItem1" />        <item android:id="@+id/groupItem2"              android:title="@string/groupItem2" />    </group></menu>

  • Checkable menu items

You can define the checkable behavior for individual menu items using theandroid:checkableattribute in the<item>element, or for an entire group with theandroid:checkableBehaviorattribute in the<group>element. For example, all items in this menu group are checkable with a radio button:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <group android:checkableBehavior="single">        <item android:id="@+id/red"              android:title="@string/red" />        <item android:id="@+id/blue"              android:title="@string/blue" />    </group></menu>

哎,今天先写到这了,有机会下次再介绍,谢谢!


更多相关文章

  1. Android使用View类动画
  2. Android(安卓)studio 点击按钮 改变viewtext 文字的颜色
  3. Android将需要的日志文件LOG记录到本地文件夹下指定的文件
  4. android跳转到卸载页面
  5. android API Demo之使用ViewFlipper制作轮播广告
  6. Android使用SAX解析XML(4)
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. android app 开发过程中 对于性能优化的
  2. 【Activity&Task&Stack】Android 中 Acti
  3. 实例+详解剖析Android之自定义View
  4. 最近学习Android的一些体会
  5. 利用Android SDK免root权限抓屏
  6. 八款常见的Android游戏引擎
  7. 2017 Android暑期实习生面试经验谈
  8. Android利用Handler异步获取子线程中的产
  9. Android 中的缓存机制与实现
  10. 十年心得:一个优秀Android开发人员必须注