注意,引用 http://developer.android.com/guide/topics/resources/menu-resource.html
Menu在资源文件中的配置如下
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:onClick="method name" android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="class name" android:actionProviderClass="class name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" /> <group android:id="@[+][package:]id/resource name" android:checkableBehavior=["none" | "all" | "single"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" > <item /> </group> <item > <menu> <item /> </menu> </item> </menu>
稍后介绍 资源文件menu的配置说明
在AppCompatActivity中关于Menu的四个方法和一个注意
1.onCreateOptionsMenu 仅仅执行一次
/**
* Initialize the contents of the Activity's standard options menu. You
* should place your menu items in to <var>menu</var>.
*
* <p>This is only called once, the first time the options menu is
* displayed. To update the menu every time it is displayed, see
* {@link #onPrepareOptionsMenu}.
*
* <p>The default implementation populates the menu with standard system
* menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
* they will be correctly ordered with application-defined menu items.
* Deriving classes should always call through to the base implementation.
*
* <p>You can safely hold on to <var>menu</var> (and any items created
* from it), making modifications to it as desired, until the next
* time onCreateOptionsMenu() is called.
*
* <p>When you add items to the menu, you can implement the Activity's
* {@link #onOptionsItemSelected} method to handle them there.
*
* @param menu The options menu in which you place your items.
*
* @return You must return true for the menu to be displayed;
* if you return false it will not be shown.
*
* @see #onPrepareOptionsMenu
* @see #onOptionsItemSelected
*/
public boolean onCreateOptionsMenu(Menu menu) {
2.onPrePareOptionsMenu 用于更新菜单按钮
/**
* Prepare the Screen's standard options menu to be displayed. This is
* called right before the menu is shown, every time it is shown. You can
* use this method to efficiently enable/disable items or otherwise
* dynamically modify the contents.
*
* <p>The default implementation updates the system menu items based on the
* activity's state. Deriving classes should always call through to the
* base class implementation.
*
* @param menu The options menu as last shown or first initialized by
* onCreateOptionsMenu().
*
* @return You must return true for the menu to be displayed;
* if you return false it will not be shown.
*
* @see #onCreateOptionsMenu
*/
public boolean onPrepareOptionsMenu(Menu menu) {}
3.onOptionsItemSelected 菜单条目点击
/**
* This hook is called whenever an item in your options menu is selected.
* The default implementation simply returns false to have the normal
* processing happen (calling the item's Runnable or sending a message to
* its Handler as appropriate). You can use this method for any items
* for which you would like to do processing without those other
* facilities.
*
* <p>Derived classes should call through to the base class for it to
* perform the default menu handling.</p>
*
* @param item The menu item that was selected.
*
* @return boolean Return false to allow normal menu processing to
* proceed, true to consume it here.
*
* @see #onCreateOptionsMenu
*/
public boolean onOptionsItemSelected(MenuItem item) {}
4.onOptionsMenuClose 菜单关闭
/**
* This hook is called whenever the options menu is being closed (either by the user canceling
* the menu with the back/menu button, or when an item is selected).
*
* @param menu The options menu as last shown or first initialized by
* onCreateOptionsMenu().
*/
public void onOptionsMenuClosed(Menu menu) {}
5.Fragment 中更改菜单时需要在 onCreate()中设置 setHasOptionsMenu(true);
Menu中的SearchView的使用
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint("请输入关键字");
searchView.setIconifiedByDefault(true);
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(MainActivity.this,query,Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(MainActivity.this,newText,Toast.LENGTH_SHORT).show();
return false;
}
});
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(MainActivity.this, "Toast", Toast.LENGTH_SHORT).show();
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(MainActivity.this, "Toast1", Toast.LENGTH_SHORT).show();
return true; // Return true to expand action view
}
};
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
Intent myShareIntent = new Intent(Intent.ACTION_SEND);
myShareIntent.setType("image/*");
myShareIntent.putExtra(Intent.EXTRA_STREAM, "http://www.baidu.com");
myShareActionProvider.setShareIntent(myShareIntent);
return super.onCreateOptionsMenu(menu);
}