The fragment content in the previous series, the Action bar is also an important point of knowledge in Android 3.0 in addition to fragment. We often use the action bar instead of the traditional title bar. If it's an Android tablet, the larger the screen, the more content and ease of use of the title you can design with the action Bar.
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 an interactive active view instead of an option entry.
5. Use the program's icon as the back home screen or up navigation action.
Tip: There are a few points to note when applying Actionbar in your program, and the SDK and final firmware must be Android 3.0 or honeycomb. Add Android:minsdkversion or android:targetsdkversion to the USES-SDK element in the Androidmanifest.xml file, similar to this:
xml/html Code
- < 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:
xml/html Code
- < 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.
The following is a menu layout file code:
xml/html 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:
Java code
- @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:
Java code
- @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:
xml/html Code
- <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 is written like this:
xml/html Code
- <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.
Java code
- @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);
- }