See https://developer.android.com/training/basics/actionbar/adding-buttons.html
1. Set the Actionbar item in the XML file
- The root node of the file is menu and placed under the Res/menu folder
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/action_search" android:icon= "@drawable/ic_action_ Search " android:title=" Find " android:showasaction=" Ifroom "/> <item android:id= "@+id/action_setting" android:title= "Settings" android:showasaction = "Never"/></menu>
2. Display Actionbar
- Actionbar has been added by default in later versions of android3.0, we only need to display the actions defined above
- The inflater here is dedicated to the Menuinflater.
@Override publicboolean oncreateoptionsmenu (Menu menu) { this. Getmenuinflater (); Inflater.inflate (r.menu.actionbar_actions, menu); return Super . Oncreateoptionsmenu (menu); }
3. Add a key response
@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.action_search:toast.maketext ( This, "Looking for ... ", Toast.length_short). Show (); Break; CaseR.id.action_overflow:toast.maketext ( This, "More Options ... ", Toast.length_short). Show (); } return Super. onoptionsitemselected (item); }4. Add return main activity function
- You need to indicate in the configuration list that the parent activity:parent Activity does not need to change
- You need to call the method to display the up key (back to main activity) in Actionbar
- We do not need to define the up key handling method
<activity android:name= ". Actionbaractivity " android:parentactivityname=" Com.njulya.demo_view. Mainactivity "> <meta-data android:name=" Android.support.PARENT_ACTIVITY " Android:value= "Com.njulya.demo_view. Mainactivity "/> </activity>
@Override publicboolean oncreateoptionsmenu (Menu menu) { this. Getmenuinflater (); Inflater.inflate (r.menu.actionbar_actions, menu); // Add return main activity function Getactionbar (). setdisplayhomeasupenabled (true); return Super . Oncreateoptionsmenu (menu); }
5. Add a common Searchview control
- You need to set the Actionviewclass property
- See http://developer.android.com/guide/topics/ui/actionbar.html
<item android:id= "@+id/action_search" android:icon= "@drawable/ic_action_search" Android:title= "Find" android:showasaction= "Ifroom|collapseactionview" Android:actionviewclass= "Android.widget.SearchView"/>
How to create Actionbar Android