/**Copyright(C)2013TheAndroidOpenSourceProject**LicensedundertheApacheLicense,Version2.0(the"License");*youmaynotusethisfileexceptincompliancewiththeLicense.*YoumayobtainacopyoftheLicenseat**http://www.apache.org/licenses/LICENSE-2.0**Unlessrequiredbyapplicablelaworagreedtoinwriting,software*distributedundertheLicenseisdistributedonan"ASIS"BASIS,*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.*SeetheLicenseforthespecificlanguagegoverningpermissionsand*limitationsundertheLicense.*/packagecom.example.android.actionbarcompat.basic;importandroid.os.Bundle;importandroid.support.v4.view.MenuItemCompat;importandroid.support.v7.app.ActionBarActivity;importandroid.view.Menu;importandroid.view.MenuItem;/***ThissampleshowsyouhowtouseActionBarCompattocreateabasicActivitywhichdisplays*actionitems.Itcoversinflatingitemsfromamenuresource,aswellasaddinganitemincode.**ThisActivityextendsfrom{@linkActionBarActivity},whichprovidesallofthefunction*necessarytodisplayacompatibleActionBarondevicesrunningAndroidv2.1+.*/publicclassMainActivityextendsActionBarActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.sample_main);}//BEGIN_INCLUDE(create_menu)/***Usethismethodtoinstantiateyourmenu,andaddyouritemstoit.You*shouldreturntrueifyouhaveaddeditemstoitandwantthemenutobedisplayed.*/@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflateourmenufromtheresourcesbyusingthemenuinflater.getMenuInflater().inflate(R.menu.main,menu);//Itisalsopossibleadditemshere.Useageneratedidfrom//resources(ids.xml)toensurethatallmenuidsaredistinct.MenuItemlocationItem=menu.add(0,R.id.menu_location,0,R.string.menu_location);locationItem.setIcon(R.drawable.ic_action_location);//NeedtouseMenuItemCompatmethodstocallanyactionitemrelatedmethodsMenuItemCompat.setShowAsAction(locationItem,MenuItem.SHOW_AS_ACTION_IF_ROOM);returntrue;}//END_INCLUDE(create_menu)//BEGIN_INCLUDE(menu_item_selected)/***Thismethodiscalledwhenoneofthemenuitemstoselected.Theseitems*canbeontheActionBar,theoverflowmenu,orthestandardoptionsmenu.You*shouldreturntrueifyouhandletheselection.*/@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseR.id.menu_refresh://Herewemightstartabackgroundrefreshtaskreturntrue;caseR.id.menu_location://HerewemightcallLocationManager.requestLocationUpdates()returntrue;caseR.id.menu_settings://Herewewouldopenupoursettingsactivityreturntrue;}returnsuper.onOptionsItemSelected(item);}//END_INCLUDE(menu_item_selected)}

menu的main.xml

<?xmlversion="1.0"encoding="utf-8"?><!--Copyright2013TheAndroidOpenSourceProjectLicensedundertheApacheLicense,Version2.0(the"License");youmaynotusethisfileexceptincompliancewiththeLicense.YoumayobtainacopyoftheLicenseathttp://www.apache.org/licenses/LICENSE-2.0Unlessrequiredbyapplicablelaworagreedtoinwriting,softwaredistributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.SeetheLicenseforthespecificlanguagegoverningpermissionsandlimitationsundertheLicense.--><!--Aswe'reusingActionBarCompat,anyactionitemattributescomefromActionBarCompat'sXMLnamespaceinsteadoftheandroidnamespace.Herewe'veaddedanewsupportnamespaceaddedtothemenuelementallowingustousethe'showAsAction'attributeinabackwardscompatibleway.Anyotheractionitemattributesusedshouldbereferencedfromthisnamespacetoo(actionProviderClass,actionViewClass,actionLayout).--><menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:support="http://schemas.android.com/apk/res-auto"><!--Herewecreateanitem,settingsupport:showAsActiontodisplaytheitemasanactionifthere'sroomonthecompatibleActionBar.--><itemandroid:id="@+id/menu_refresh"android:icon="@drawable/ic_action_refresh"android:title="@string/menu_refresh"support:showAsAction="ifRoom"/><!--LocationitemisaddedinonCreateOptionsMenu()--><!--Herewesetthesettingsitemtoalwaysbeintheoverflowmenu,bysettingsupport:showAsActiontonever,soitisneverdisplayedasanactionitemonthecompatibleActionBar.--><itemandroid:id="@+id/menu_settings"android:icon="@drawable/ic_action_settings"android:title="@string/menu_settings"support:showAsAction="never"/></menu>

下载地址:http://down.51cto.com/data/2261370