1.Options Menu.
通过按下手机上的MENU键,可以打开Options Menu。Options Menu最多只能在屏幕最下面显示6个菜单项,称为Icon Menu.如果添加了多于6个的菜单项,将通过"More"菜单项显示,这种称为Expanded Menu.当Options Menu第一次被打开时,系统将调用Activity的onCreateOptionsMenu(Menu menu)方法,在这个方法中可以通过配置一个XML文件,或者调用Menu的add()方法来添加你想要的Menu。Menu的add()方法将返回一个MenuItem对象,你用通过这个对象来配置一些其他属性。比如:icon,shortcut,intent。Menu的add()方法很多,要注意的是itemId这个参数,它的唯一性。
当选择了一个Options Menu时,会调用onOptionsItemSelected(MenuItem item)方法。如果想修改Options Menu,需要重写onPrepareOptionsMenu()方法。
用add()添加:
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_NEW_GAME, 0, "New Game");
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
return true;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_NEW_GAME:
newGame();
return true;
case MENU_QUIT:
quit();
return true;
}
return false;
}
通过XML文件添加:
在res/menu/下新建options_menu.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:title="New Game" />
<item android:id="@+id/quit"
android:title="Quit" />
</menu>
在onCreateOptionsMenu()方法中,我们这样写:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
2.Context Menu.
它需要注册到某个View对象上,当长按这个View大概2秒时间,会出现这个Context Menu。为某个View注册Context Menu是用registerForContextMenu(View view)方法来实现。Context Menu在显示前都会调用onCreateContextMenu来生成menu。onContextItemSelected用来处理选中的菜单项。注意:Context Menu不支持icons和shortcut keys.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, EDIT_ID, 0, "Edit");
menu.add(0, DELETE_ID, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case EDIT_ID:
editNote(info.id);
return true;
case DELETE_ID:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
注册这个Context Menu:
registerForContextMenu(getListView());
这里的 getListView()
是ListActivity的方法,它将返回一个ListView.记住任何一个View对象都可以注册一个
Context Menu。
3.Submenus
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
SubMenu fileMenu = menu.addSubMenu("File");
SubMenu editMenu = menu.addSubMenu("Edit");
fileMenu.add("new");
fileMenu.add("open");
fileMenu.add("save");
editMenu.add("undo");
editMenu.add("redo");
return result;
}
4.Menu Features
Menu groups 菜单项分组的功能:
在用Menu的add()方法时,通过参数groupId的设置可以实现分组。setGroupVisible()用来显示或者隐藏你的菜单组。setGroupEnabled()用来启用或者禁用你的菜单组。setGroupCheckable()用来设置你的菜单项是否可选。 setGroupCheckable()
方法有三个参数int group, boolean checkable, boolean exclusive。第一个参数是要设置的组,第二个参数是否可选,第三个参数为true表示单选,false表示多选。
Shortcut keys 快捷键:
我们还可以设置快捷键。使用setAlphabeticShortcut(char),setNumericShortcut(int),setShortcut(char,int)可以设置字符,数字,组合(字符+数字)键。注意:Context Menu不能添加快捷键。
分享到:
相关推荐
原创讲解Android Menus, 包括各种类型的menus
Simple material-style flyout menus for Android. There are many flyout type menus for Android, but this one is mine. compile 'org.zakariya.flyoutmenus:flyoutmenu:0.5.3' minSdkVersion: 14 Note: when ...
文章http://blog.csdn.net/Jason69181/archive/2010/12/11/6069673.aspx完整代码.
官方版本,亲测可用
官方版本,亲测可用
【Android开发API】用户界面-菜单-Menus.pdf
android-tabs-menus
You will also create beautiful and responsive menus and dialogs and explore the different options for playing sound effects and music in Android. You will then learn the basics of creating a ...
Chapter 8: Working with Input Methods, Menus and Dialogs Chapter 9: Adopting the Action Bar Chapter 10: The Life of an Activity Part III: A World of Wonderful Devices and Screens Chapter 11: Android ...
Working with Menus 162 Working with XML Files 163 Working with Raw Files 164 References to Resources 165 Working with Layouts 166 Referencing System Resources 171 Summary 172 Quiz Questions 173 ...
8 Lists, Menus, and Other Views . 149 9 Using the Cell Phone’s GPS Functionality . . 203 10 Using the Google API with GTalk . . . . 239 11 Application: Find a Friend 257 12 Android SDK Tool ...
47. CreatingandManaaing overflow Menus. on Android 48. Animating User Interfaceswith the Android Transitions Framework 49. An Android Transition Tutorial using beginDelayedTransition sI wWatinawthbthe...
官方版本,亲测可用
* Deploy menus, progress bars, and auto-complete functions * Trigger actions using Android Intents, Filters, and Receivers * Implement GPS, Google Maps, Google Earth, and GTalk * Build ...
This Android library makes it very easy to create tree menus in an Android APP . You need not write xml files any more to create tree menus By using TreeMenu. You can easily create a three-layer tree...
Build UIs in Android using layouts, widgets, lists, menus, and action bars Deal with data in your Android apps using data persistence and cloud access Design for different Android devices Create ...
android好书系列,我目前找了两本,先贡献给大家。以后如果有好的资源,再上传和大家分享。 本资源是第一本。 这是一本学习android的好书,我浏览了,是老外著的,高清晰的pdf。为了方便大家学习android,我将本书所...
Chapter 7 covers all the pop-up mechanisms—menus, dialogs, and toasts—and one that doesn’t pop up but is also for interaction outside your application’s window, Android’s notification mechanism. ...
android平台下各种菜单,上下文菜单,Options menus等
菜单、弹出窗口和对话框的 Android 演示。 有关更多详细信息,请参阅悬崖注释。 特征: 显示带有布局内容的自定义对话框 使用警报生成器显示自定义对话框 显示弹出菜单以显示次要操作 显示工具提示的弹出窗口 为...