android小结(一)之menu

android提供有三种menu类型
一.Options Menu(选项菜单)
这是一组item选项的集合,当用户点击MENU button时,此menu就会出现。如果是3.0以后的版本可以通过action bar直接选择menu item
二.Context Menu(上下文菜单)
当用户长按住一个注册了上下文菜单的控件时,会弹出一个上下文菜单,它是一个流式的列表,供用户选择某项
三.Submenu(子菜单)
一个item项可以包含一个内嵌的子菜单


1.下面讲如何创建一个Options Menu菜单
通过xml布局文件创建菜单项

<?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>

调用:重写acitvity的onCreateOptionsMenu方法创建菜单

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}

你也可以直接调用menu的add方法直接添加菜单项。

options menu响应用户事件需重写activity的onOptionsItemSelected

@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);
}
}

根据item id分别对选中某选项进行处理。return true表示自己已经处理此方法,不需要父类onOptionsItemSelected进行处理。
如果你有多个activity都有相同的menu,可以写一个activity实现onCreateOptionsMenu() and onOptionsItemSelected() 方法,然后其他activity继承此activity

[b]在运行时改变menu item的状态[/b]
你需要重写onPrepareOptionsMenu() ,它方便你对item移除,添加,使不可用等
在android 2.3以下,当用户要每次打开menu时就会调用onPrepareOptionsMenu()方法
在android 3.0以上,你必须调用invalidateOptionsMenu() 当你要update你的menu时,因为
action bar是一直出现的。然后系统将调用onPrepareOptionsMenu()更改menu


2.创建一个Context Menu
上下文菜单相当于pc机上的点击鼠标右键弹出框。当用户长按一选项时,上下文菜单就会出现。你能为任何view创建一个上下文菜单,尽管Context Menu多用于listview。
要创建一个Context Menu,你首先必须为你的控件注册registerForContextMenu() ,并重写
onCreateContextMenu() and onContextItemSelected().

@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);
}

这里使用布局文件创建菜单项,跟options menu差不多。当然你同样可以使用menu.add()方法创建items。ContextMenu.ContextMenuInfo 对象将提供你选择的选项的额外信息,比如id

@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);
}
}



3.创建submenu
A submenu is a menu that the user can open by selecting an item in another menu。
一个子菜单能够从另外一个菜单的选项中被打开。你能为任何一个menu添加子菜单,除了submenu。下面看看如何通过布局文件创建一个submenu

<?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>


You can also use addSubMenu() to dynamically add a SubMenu to an existing Menu. This returns the new SubMenu object, to which you can add submenu items, using add()

4.其他的menu特性
[b]
Menu groups[/b]
通过组菜单,你可以同时对一组item进行操作,比如setGroupVisible(),setGroupEnabled(),setGroupCheckable()
下面给出一个包含组菜单的资源文件

<?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>

3个item任然是一个兄弟菜单,不同的是你可以通过组操作一组item
[b]
Checkable menu items[/b]
通过布局文件创建Checkable menu,可以通过使用item的android:checkable属性
和group的android:checkableBehavior属性。例如

<?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>

APi中:
The android:checkableBehavior attribute accepts either:
single
Only one item from the group can be checked (radio buttons)
all
All items can be checked (checkboxes)
none
No items are checkable
由于不会自动改变checkbox的状态,你必须设置checkbox的状态通过isChecked()和setChecked().

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.vibrate:
case R.id.dont_vibrate:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

如果你不手动设置它,当你点击一个选项时,checkbox或者radioButton的状态是不会改变的

[b]Shortcut keys(热键)[/b]
你可以为你item设置快速点击热键
在配置文件中通过item的属性android:alphabeticShortcut 和 android:numericShortcut
类文件中通过setAlphabeticShortcut(char) and setNumericShortcut(char).设置的热键不区分大小写


[b]Dynamically adding menu intents[/b]
有时候你希望使用menu item来启动一个activity通过intent。你能在onOptionsItemSelected()使用startActivity()
然而如果你不确定用户的移动设备是否拥有一个应用程序来处理这个intent,那么你可以使用Dynamically adding menu intents来决定是否添加此item
一个简单的实例

@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);

// Create an Intent that describes the requirements to fulfill, to be included
// in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.
Intent intent = new Intent(null, dataUri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);

// Search and populate the menu with acceptable offering applications.
menu.addIntentOptions(
R.id.intent_group, // Menu group to which new items will be added
0, // Unique item ID (none)
0, // Order for the items (none)
this.getComponentName(), // The current activity name
null, // Specific items to place first (none)
intent, // Intent created above that describes our requirements
0, // Additional flags to control items (none)
null); // Array of MenuItems that correlate to specific items (none)

return true;
}

addIntentOptions()返回的是你添加的item的个数

[b]Allowing your activity to be added to other menus[/b]
如果允许你的activity可以被别的menu添加,必须设置CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE values for the intent filter category

<intent-filter label="Resize Image">
...
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
...
</intent-filter>

还有一个notePad的示例我在研究研究
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值