【Android】Menu不同菜单的使用介绍
16lz
2021-01-23
【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:
【1】使用XML文件来创建OptionsMenu
- 这是使用inflater XML的方式来创建一个Menu.显然我们需要在res/menu/的目录下创建类似下面的一个文件:
android:id
android:icon
android:title
- Inflating a Menu Resource
当点击menu按键的时候会call 到onCreateOptionsMenu.
- 当用户点击其中的item的适合会call到下面的方法:
- 根据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()
,这个方法传入一个已经存在的Menu,之后我们可以进行remove, add, disable, or enable menu items.onPrepareOptionsMenu()
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. 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 theView
you 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 aListView
and you want all list items to provide a context menu, register all items for a context menu by passing theListView
toregisterForContextMenu()
. For example, if you're using aListActivity
, register all list items like this: registerForContextMenu(getListView()
);
实现创建Context Menu,我们需要用到onCreateContextMenu()
andonContextItemSelected()
.
Note:Items in a context menu do not support icons or shortcut keys.
【3】Creating Submenus 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 the
onOptionsItemSelected()
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 with
setGroupVisible()
- Enable or disable all items with
setGroupEnabled()
- Specify whether all items are checkable with
setGroupCheckable()
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.
- Checkable menu items
You can define the checkable behavior for individual menu items using theandroid:checkable
attribute in the<item>
element, or for an entire group with theandroid:checkableBehavior
attribute in the<group>
element. For example, all items in this menu group are checkable with a radio button: 哎,今天先写到这了,有机会下次再介绍,谢谢!
更多相关文章
- android 登陆右上角 的关闭按钮的使用方法
- Android AESUtils 工具类与调用方法
- sqlit导入外部数据库查找数据方法
- Android NDK打印log到logcat的方法
- Android获取系统时间的多种方法
- Android链式方法显示Dialog
- 【Android M】Monkey命令源码及是否处于monkey测试的判断方法
- Bugly Android 这个错误 Cleartext HTTP traffic to android.bug