标签:
<menu><item><group>game_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"android:showAsAction="ifRoom"/><item android:id="@+id/help"android:icon="@drawable/ic_help"android:title="@string/help" /></menu>
android:idandroid:iconandroid:titleandroid:showAsAction<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/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>


onCreateOptionsMenu() (fragment
提供了onCreateOptionsMenu()回调方法)。在该方法中,你可以解析你的菜单资源文件(在XML中定义)到Menu中,该menu由回调函数提供。如下:@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.game_menu, menu);return true;}
onOptionsItemSelected() 方法。该方法传递了被选中的menuItem。你可以通过调用getItemId()来识别item,该方法返回一个菜单项的唯一ID(通过资源文件下的android:id 或在add() 方法中给定的integer数据
定义)。你可以匹配这个ID来了解菜单项用来执行的适当操作。如:@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle item selectionswitch (item.getItemId()) {case R.id.new_game:newGame();return true;case R.id.help:showHelp();return true;default:return super.onOptionsItemSelected(item);}}
onOptionsItemSelected() (该默认实现返回的是false)onCreateOptionsMenu()and onOptionsItemSelected() 方法之外,什么方法都不去实现。然后让每个可能有相同选项菜单的activity
都继承该类。这样,你可以组织一个代码集合来处理菜单操作和每一个子类的菜单行为。如果你想在每个子类中添加菜单项,在相应的activity 中覆盖onCreateOptionsMenu() activity
调用super.onCreateOptionsMenu(menu) 这样原始的菜单项就被创建了,然后通过menu.add() 添加新的菜单项。你也可以为每一个菜单项覆盖父类的行为。onPrepareOptionsMenu() 方法中做些什么事情。该方法传递你的menu对象作为当前状态,所以你可以更改它,例如增加,移除或取消items(fragments
提供了onPrepareOptionsMenu() 方法)onPrepareOptionsMenu().
ListView, GridViewother 或其它
可集合的view上,这样用户可以为每个项目直接执行操作。registerForContextMenu() 并且传递这个view。ListView 或GridView 并且你想要为每一项都提供相同的上下文菜单,通过调用registerForContextMenu().传递该 ListView or GridView 来在上下文菜单中注册所有的项目。Activity 或Fragment.中实现onCreateContextMenu() 方法。当被注册的view接受到一个长按事件,系统调用你的onCreateContextMenu()方法。这是你定义菜单项的地方,通常通过展开一个菜单资源文件。如下:@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.context_menu, menu);}
MenuInflater 允许你从一个菜单资源中展开该上下文菜单。该回调方法中包含一个用户选中的view和一个关于提供了被选择项的额外消息的ContextMenu.ContextMenuInfo 对象。如果你的activity有多个view,并且都提供了不同的上下文菜单,你或许要使用这些参数来决定展开哪个上下文菜单。@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);}}
ActionMode.Callback 接口。在它的回调方法中,你可以为contextual
action bar 指定操作,响应action item的点击事件,并且处理其它生命周期事件。private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {// Called when the action mode is created; startActionMode() was called@Overridepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {// Inflate a menu resource providing context menu itemsMenuInflater inflater = mode.getMenuInflater();inflater.inflate(R.menu.context_menu, menu);return true;}// Called each time the action mode is shown. Always called after onCreateActionMode, but// may be called multiple times if the mode is invalidated.@Overridepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {return false; // Return false if nothing is done}// Called when the user selects a contextual menu item@Overridepublic boolean onActionItemClicked(ActionMode mode, MenuItem item) {switch (item.getItemId()) {case R.id.menu_share:shareCurrentItem();mode.finish(); // Action picked, so close the CABreturn true;default:return false;}}// Called when the user exits the action mode@Overridepublic void onDestroyActionMode(ActionMode mode) {mActionMode = null;}};
ActionMode api为CAB来制作不同的变化,如
使用setTitle() 和setSubtitle()修改标题和副标题(指明多少项被选择了)。someView.setOnLongClickListener(new View.OnLongClickListener() {// Called when the user long-clicks on someViewpublic boolean onLongClick(View view) {if (mActionMode != null) {return false;}// Start the CAB using the ActionMode.Callback defined abovemActionMode = getActivity().startActionMode(mActionModeCallback);view.setSelected(true);return true;}});
startActionMode(),系统返回创建的ActionMode。将这些存储在一个成员变量中,你可以为上下文相关action
bar响应其它事件而做出相应的改变。在上面的示例中,该ActionMode 被用来确保该ActionMode 引用没有被重复创建,通过在启动action
mode前检查成员是否为空。setMultiChoiceModeListener().为view
group 进行设置。在监听器的回调函数中,你可以为contextual action bar 指定操作,响应项目的点击事件,并且处理其它继承自ActionMode.Callback 接口的回调。ListView listView = getListView();listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {@Overridepublic void onItemCheckedStateChanged(ActionMode mode, int position,long id, boolean checked) {// Here you can do something when items are selected/de-selected,// such as update the title in the CAB}@Overridepublic boolean onActionItemClicked(ActionMode mode, MenuItem item) {// Respond to clicks on the actions in the CABswitch (item.getItemId()) {case R.id.menu_delete:deleteSelectedItems();mode.finish(); // Action picked, so close the CABreturn true;default:return false;}}@Overridepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {// Inflate the menu for the CABMenuInflater inflater = mode.getMenuInflater();inflater.inflate(R.menu.context, menu);return true;}@Overridepublic void onDestroyActionMode(ActionMode mode) {// Here you can make any necessary updates to the activity when// the CAB is removed. By default, selected items are deselected/unchecked.}@Overridepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {// Here you can perform updates to the CAB due to// an invalidate() requestreturn false;}});

Menu对象中去,该对象通过PopupMenu.getMenu()获得。在API
14和之前版本,你可以利用PopupMenu.inflate() 替代。android:onClick 属性来显示弹出菜单:<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_overflow_holo_dark"android:contentDescription="@string/descr_overflow_button"android:onClick="showPopup" />
public void showPopup(View v) {PopupMenu popup = new PopupMenu(this, v);MenuInflater inflater = popup.getMenuInflater();inflater.inflate(R.menu.actions, popup.getMenu());popup.show();}
PopupMenu.inflate().public void showMenu(View v) {PopupMenu popup = new PopupMenu(this, v);// This activity implements OnMenuItemClickListenerpopup.setOnMenuItemClickListener(this);popup.inflate(R.menu.actions);popup.show();}@Overridepublic boolean onMenuItemClick(MenuItem item) {switch (item.getItemId()) {case R.id.archive:archive(item);return true;case R.id.delete:delete(item);return true;default:return false;}}
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/menu_save"android:icon="@drawable/menu_save"android:title="@string/menu_save" /><!-- menu group --><group android:id="@+id/group_delete"><item android:id="@+id/menu_archive"android:title="@string/menu_archive" /><item android:id="@+id/menu_delete"android:title="@string/menu_delete" /></group></menu>
android:showAsAction="ifRoom" ,他们要么同时显示在action
bar 要么同时出现在溢出菜单。
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>
singleallnonesetChecked().设置状态。如下:@Overridepublic 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);}}
Menu.addIntentOptions().。安卓系统之后会寻找符合intent的应用并且将它们加入你的菜单。@Overridepublic 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 added0, // Unique item ID (none)0, // Order for the items (none)this.getComponentName(), // The current activity namenull, // Specific items to place first (none)intent, // Intent created above that describes our requirements0, // Additional flags to control items (none)null); // Array of MenuItems that correlate to specific items (none)return true;}
android:label 作为菜单项的标题,应用图标作为菜单项的图标。addIntentOptions()方法返回被添加的菜单项的数量。CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE 值。如下:<intent-filter label="@string/resize_image">...<category android:name="android.intent.category.ALTERNATIVE" /><category android:name="android.intent.category.SELECTED_ALTERNATIVE" />...</intent-filter>
标签:
原文地址:http://blog.csdn.net/yh_android_blog/article/details/51980649