1 Define the Options menu in the XML file:
Create the Res/menu directory and add the XML file: Select New-Android xmlfiles. Select the menu file resource type;
Open the new XML file, switch to XML edit view, and add a new Item element:
<?XML version= "1.0" encoding= "Utf-8"?><Menuxmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_item_new_crime" android:icon = "@android:d rawable/ic_menu_add" android:title= "@string/new_crime" Android:showas Action= "Ifroom|withtext" /> </Menu>
Creates a new resource ID for item, which is used in the response code that follows. The Showasaction property is used to specify whether the menu item is displayed on the action bar or hidden in the overflow menu. Select the Ifroom and Withtext combined values to indicate that if there is enough space, the menu item and text caption will be displayed, and the text caption will not be displayed if the space is only sufficient to display the icon. How to access the overflow menu depends on the specific device.
2 Creating the Options menu:
When the Options menu is needed, Android invokes the activity's Oncreateoptionmenu method. But fragment also has its own Options menu callback function:
Public void Oncreateoptionsmenu (Menu menu, Menuinflater inflater) Public boolean onoptionsitemselected (MenuItem Item)
First, the Oncreateoptionsmenu method is overridden in the fragment class, and a menu is created from the parameter inflater:
@Override Public void // This method is called by the Fragmentmanager that hosts the activity // TODO auto-generated Method stub Super . Oncreateoptionsmenu (menu, inflater); Inflater.inflate (r.menu.fragment_crime_list, menu);}
The Oncreateoptionsmenu method that calls the superclass belongs to the programming specification, although it does nothing inside. To inform the fragmentmanager of the managed activity that it has an options menu, you need to call the Sethasoptionsmenu method:
@Override Public voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Sethasoptionsmenu ( true); //The Oncreateoptionsmenu method is called by the Fragmentmanager hosting the activity,//need to use Sethasoptionsmenu method to inform FragmentmanagerGetactivity (). Settitle (R.string.crimes_title);//getactivity () return to managed activity//crimelistfragment to display a list of crime in Crimelab, you need to get a list of the Crimelab singleton objects firstMcrimes =Crimelab.get (Getactivity ()). Getcrimes (); Crimeadapter Adapter=NewCrimeadapter (mcrimes); //ListView <--> Adapter <--> mcrimesSetlistadapter (adapter);//set adapter for Crimelistfragment built-in ListView}
3 Response Menu Selection:
To respond to the selection of the Options menu, you need to overwrite onoptionsitemselected:
@Override public boolean onoptionsitemselected (MenuItem item) { // TODO auto-generated method stub case R.id.menu_item_new_crime: {
//Todo:add Your code ...
return true ; default : return super .onoptionsitemselected ( Item); }}
Where R.id.menu_item_new_crime is the ID of the option menu item that was created at the beginning, and returns true to indicate that all the response tasks have been processed, and that if item is not in its own relationship, you can go to the default branch and let the superclass handle it.
Additional content:
How do I add a hierarchical navigation to an application?
Now many applications, in the Options menu where there is an upward level navigation button, the following gives the most general implementation ideas.
1 enable the function of the Apply icon up navigation button:
In the Fragment.oncreateview method, call the Setdisplayhomeasupenable method to set the properties of the fragment.
@TargetApi (11) @Override PublicView Oncreateview (Layoutinflater inflater, viewgroup parent, Bundle savedinstancestate) {//Step4//TODO auto-generated Method StubView v = inflater.inflate (r.layout.fragment_crime, parent,false); //enable the UP navigation button if(Build.VERSION.SDK_INT >=Build.version_codes. Honeycomb) {if(Navutils.getparentactivityname (getactivity ())! =NULL) {getactivity (). Getactionbar (). setdisplayhomeasupenabled (true); }}
At this point, the icon on the application icon will appear to the left, but click the button, no response.
2 Response up Button:
Overriding the Fragment.onoptionsitemselected method:
@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseAndroid. R.id.home: {if(Navutils.getparentactivityname (getactivity ())! =NULL{//First to determine if there is a parent activity, there is a complete jump, this parent-child relationship through manifest registration Navutils.navigateupfromsametask (getactivity ()); } return true; } default: { return Super. onoptionsitemselected (item); } }}
Here is the Navutils class that completes the navigation function to jump from one activity to another. This source-to-destination relationship is registered through the manifest file, for example, jumping from activity_a to Activity_b, which can be registered in manifest:
<android:name= ". Activity_a " android:label=" @string/app_name "> < android:name= "Android.support.PARENT_ACTIVITY" android:value= ". Activity_b "/></activity>
[Android] (Learn note 7) Add the Options menu to your application