Reprint Please specify source: http://blog.csdn.net/yangyu20121224/article/details/9042387
Because it is just beginning to write a blog, so at the beginning of the format is not too concerned about, today by chance to read their own blog, but found that fonts, code and pictures are almost squeezed together, they feel very uncomfortable, let alone other readers. For everyone to be able to read clearly and see, today the blogger took a little time to change the title and format of the previous articles of this topic all over again. Hope readers continue to support, your support is my biggest motivation!
I. Introduction of Actionbar
In addition to the fragment we focused on in Android 3.0, Action Bar is also a very important interaction element, with the action Bar replacing the traditional tittle bar and menu, which has been placed at the top of the program running, Bigger screen for Android tablet the title uses the action bar to design more rich content for easy manipulation.
Second, the function of Actionbar
To explain its function in a graphic way.
<1> can display software icons, and other icons may be used instead. When the software is not on the top page, a left arrow appears to the left of the icon, which allows the user to navigate up the arrow;
<2> If your app wants to display data in a different view, this section allows the user to switch views. The general practice is to use a drop-down menu or Tab tab. If there is only one interface, then this can show the application title or a longer trademark information;
<3> here to put the important button function, to provide users with a direct access to the operation;
The <4> button will be placed in the "more ..." menu item, "More ..." is implemented in the following dropdown form.
Third, the use of Actionbar precautions
1, get Actionbar
In Android 3.0 and later, the activity contains the Actionbar component by default. An app that is thought to be based on the Android 3.0 logo is set in the <uses-sdk> tab in Androidmanifest android:minsdkversion or Android: When the Targetsdkversion property has a value of 11 or greater, the application at this time is considered an app on Android 3.0.
<span style= "FONT-SIZE:12PX;" ><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = " Com.example.helloworld " android:versioncode=" 1 " android:versionname=" 1.0 "> <USES-SDK android:minsdkversion= "4" android:targetsdkversion= "one"/> <application ... > ... </application></manifest></span>
2. Cancellation of Actionbar
If you need to hide the action bar you can 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 the Actionbar, and the code is:
<span style= "FONT-SIZE:12PX;" ><activity android:theme= "@android: Style/theme.notitlebar" ></span>
There is also a way to call the Hide () method at run time to hide Actionbar and call the show () method to display Actionbar (). For example:
ActionBar ActionBar = getactionbar (); Actionbar.hide ();
When you hide Actionbar, the system fills the entire space with the activity's entire content. Note: If you use a theme (theme) to remove Actionbar from activity, the window will no longer have actionbar, so there is no way to add actionbar--call Getactionbar () at run time Method will return a null value. Iv. Example five, project directory structure 1, add an activity entry in the layout file of the menu action items and an active view action View,main.xml:
<span style= "FONT-SIZE:12PX;" ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item Android:id = "@+id/menu_add" android:icon= "@drawable/ic_title_share_default" android: Showasaction= "Ifroom|withtext" android:title= "Share"/> <item Android:id = "@+id/menu_search" android:actionviewclass= "Android.widget.SearchView" Android:showasaction= "Ifroom" android:title= "Search"/></menu></span>
2, main interface class, Mainactivity.java:
PackageCom.yangyu.myactionbar;ImportAndroid.app.ActionBar;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ActionBar ActionBar= This. Getactionbar (); Actionbar.setdisplayoptions (actionbar.display_home_as_up, actionbar.display_home_as_up); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseAndroid. R.id.home://called when the Actionbar icon is clickedSystem.out.println ("Click the Home button! "); Break; } return Super. onoptionsitemselected (item); }}
Note: when calling Getactionbar mode in your activity's oncreate, you need to be aware that it must be after the Setcontentview has been called.
Well, write about it today, and still use a simple example to practice practiced hand, which will continue to explain more about actionbar in more detail later on.
"Android UI design and development" phase 10th: Top title bar (i) Actionbar detailed overview and simple examples