In Android 3.0, in addition to our focus on the fragment, Action Bar is also an important content, action Bar is mainly used to replace the traditional title bar, for Android tablet device, the screen is larger its title use Action Bar to design to show more rich content, easy to control.
The main functions of the Action bar include:
1. Display the Options menu
2. Provide the navigation function of the switch mode of the tab page, can switch multiple fragment.
3. Provide a drop-down navigation entry.
4. Provide interactive activity view instead of option entry
5. Use the program's icon as the back home screen or up navigation action.
Tips for applying Actionbar in your program there are a few points to note, the SDK and the final running firmware must be Android 3.0, or honeycomb, to add Android to the USES-SDK element in the Androidmanifest.xml file: Minsdkversion or android:targetsdkversion, similar
- < manifest xmlns:android="Http://schemas.android.com/apk/res/android"
- package="EOE.ANDROID.CWJ"
- android:versioncode="1"
- Android:versionname="1.0" >
- < USES-SDK android:minsdkversion="Honeycomb"/>
- < Application >
- </application>
- </manifest>
If you need to hide the action bar to set the theme style for Notitlebar in your activity's properties in your manifest file, the following code hides the title before 3.0, and after 3.0 it hides Actionbar, and the code is:
- < activity android:theme="@android: Style/theme.notitlebar" >
First, add the active item action items
For the active item, you can see the right part of the title of Android 3.0 as a toolbar, and the following save and delete are the two action items activity entries.
Below is a menu layout file code
- < XML version= "1.0" encoding= "utf-8"?>
- < menu xmlns:android="Http://schemas.android.com/apk/res/android" >
- < item android:id="@+id/menu_add"
- android:icon="@drawable/ic_menu_save"
- android:title="@string/menu_save"
- android:showasaction="Ifroom|withtext"/>
- </menu>
Other code is similar to the menu in activity, such as
- @Override
- Public Boolean onoptionsitemselected (MenuItem item) {
- Switch (Item.getitemid ()) {
- Case Android. R.id.home:
- Executes the following intent when the icon of the action Bar is clicked
- Intent Intent = new Intent (This, Android123. Class);
- StartActivity (Intent);
- Break
- }
- Return super.onoptionsitemselected (item);
- }
For the creation of Actionbar, you can override the OnStart method in your activity:
- @Override
- protected void OnStart () {
- Super.onstart ();
- ActionBar ActionBar = This.getactionbar ();
- Actionbar.setdisplayoptions (actionbar.display_home_as_up, actionbar.display_home_as_up);
- }
You need to be aware that you must call Getactionbar in the oncreate of your activity when you call Setcontentview.
Second, add the active view action
For Actionview, we can customize the Searchview layout in the menu layout file using, as follows:
- < item android:id="@+id/menu_search"
- android:title="Search"
- android:icon="@drawable/ic_menu_search"
- android:showasaction="Ifroom"
- android:actionlayout="@layout/searchview"/>
You can also specify the Searchview control in the Android system directly, then the menu "_search" code to write this way:
- < item android:id="@+id/menu_search"
- android:title="Search"
- android:icon="@drawable/ic_menu_search"
- android:showasaction="Ifroom"
- android:actionviewclass="Android.widget.SearchView"/>
It is important to note that one of the two methods above is actionlayout to develop an XML layout file, one that specifies a class Actionviewclass The final call can map the menu layout in response to the Oncreateoptionsmenu method in activity.
- @Override
- Public Boolean Oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (r.menu.options, menu);
- Searchview Searchview = (searchview) menu.finditem (r.id.menu_search). Getactionview ();
- Return Super.oncreateoptionsmenu (menu);
- }
Copy to Google TranslateTranslation Results
the JAVA"In viewplaincopy
Android Actionbar (i): Actionbar Overview and its creation