【Android】Menu不同菜单的使用介绍


【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
@Override public 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到下面的方法:
@Override public 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().
@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); }

@Override public 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:

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>
哎,今天先写到这了,有机会下次再介绍,谢谢!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值