标签:
import android.support.v7.app.ActionBarActivity;
继承基类ActionBarActivity -> FragmentActivity -> Activity
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:support="http://schemas.android.com/apk/res-auto" > <!-- Here we create an item, setting support:showAsAction to display the item as an action if there‘s room on the compatible Action Bar. --> <item android:id="@+id/menu_refresh" android:icon="@drawable/ic_action_refresh" android:title="@string/menu_refresh" support:showAsAction="ifRoom"/> <!-- Location item is added in onCreateOptionsMenu() --> <!-- Here we set the settings item to always be in the overflow menu, by setting support:showAsAction to never, so it is never displayed as an action item on the compatible Action Bar. --> <item android:id="@+id/menu_settings" android:icon="@drawable/ic_action_settings" android:title="@string/menu_settings" support:showAsAction="never"/> </menu>
// BEGIN_INCLUDE(create_menu) /** * Use this method to instantiate your menu, and add your items to it. You * should return true if you have added items to it and want the menu to be displayed. */ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate our menu from the resources by using the menu inflater. getMenuInflater().inflate(R.menu.main, menu); // It is also possible add items here. Use a generated id from // resources (ids.xml) to ensure that all menu ids are distinct. MenuItem locationItem = menu.add(0, R.id.menu_location, 0, R.string.menu_location); locationItem.setIcon(R.drawable.ic_action_location); // Need to use MenuItemCompat methods to call any action item related methods MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM); return true; } // END_INCLUDE(create_menu) // BEGIN_INCLUDE(menu_item_selected) /** * This method is called when one of the menu items to selected. These items * can be on the Action Bar, the overflow menu, or the standard options menu. You * should return true if you handle the selection. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_refresh: // Here we might start a background refresh task return true; case R.id.menu_location: // Here we might call LocationManager.requestLocationUpdates() return true; case R.id.menu_settings: // Here we would open up our settings activity return true; } return super.onOptionsItemSelected(item); } // END_INCLUDE(menu_item_selected)
标签:
原文地址:http://www.cnblogs.com/xbx2015/p/4498010.html