Android Custom Actionbar

Source: Internet
Author: User

Android 3.0 and above already have the Actionbar API, which can be achieved by introducing the support The package references these APIs on platforms below 3.0, but here, fully customizing a actionbar without introducing additional jar packages, referencing the Open source UI component Greeendroid, Project home: https://github.com/ Cyrilmottier/greendroid. Extract the relevant documents about Actionbar, which you can put in your own project and will be attached at the end. The following is the usage in the program.

Create a new Testactionbar project, assuming you put the relevant code under the Com.leaf.actionbar package. First create a new layout file, Main.xml, as follows:

<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:bar="Http://schemas.android.com/apk/res/com.leaf.actionbar"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <Com.leaf.actionbar.ActionBar Android:id="@id/gd_action_bar"Android:layout_height="@dimen/gd_action_bar_height"Android:layout_width="fill_parent"Android:background="? Attr/gdactionbarbackground"Bar:type="Normal"Bar:title="Test ActionBar"/> </LinearLayout>

This custom Actionbar is actually a linearlayout, and some of its own properties, the first to define a namespace, the name is arbitrary, such as the bar above. Title is the caption of Actionbar; Type is the layout of the top left item that distinguishes this actionbar, there are three types, one is normal: The left is a ImageButton that shows the home page, and the TextView that displays the title, One is dashboard, the left is a display app icon ImageView and display the title of the TextView, one is empty, the left only a display title TextView, the right side is the button you added, the default is normal. The following: Corresponds to normal, dashboard, and empty respectively.

There are some properties, homedrawable can replace the picture on the left, the default is the picture of that home page, dividerdrawable: Split Line, Dividerwidth: Split line width, Maxitems: The maximum number of items added to the right. You can choose to add.

Then Mainactivity.java

 Public classMainactivity extends Activity {PrivateActionBar Mactionbar; PrivateFinal Handler Mhandler =NewHandler (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); //for the case that type is normalMactionbar =(ActionBar) Findviewbyid (R.id.gd_action_bar); //adding the specific item to the right is actually ImageButton//it encapsulates some type inside itself, such as refresh, search, mostly directly using some of its built-in images as ImageButton src//This is followed by the ID of the ImageButton, which can be defined in the Values/ids.xml//Refresh is not the same as other type, except a ImageButton, there is a ProgressBar, below can seeMactionbar.additem (Type.refresh, R.id.action_bar_refresh);        Mactionbar.additem (Type.search, R.id.action_bar_search); //You can also add an item to your own and set your own picture as followsMactionbar.additem (Mactionbar.newactionbaritem (Normalactionbaritem.class). setdrawable (R.drawable.gd_action_bar_eye). Setcontentdescription ("View"), R.id.action_bar_view); //Add a Listen event to item, ImageButtonMactionbar.setonactionbarlistener (NewOnactionbarlistener () {@Override Public voidOnactionbaritemclicked (intposition) {                if(Position = =ActionBar.OnActionBarListener.HOME_ITEM) {                    //action triggered when you press the home button on the leftToast.maketext (mainactivity. This,"home or back", Toast.length_short). Show (); return; } Final Actionbaritem Item=Mactionbar.getitem (position); Switch(Item.getitemid ()) { CaseR.id.action_bar_refresh:if(item instanceof Loaderactionbaritem) {mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {//this way you can show and hide that ProgressBar.((Loaderactionbaritem) item). setloading (false); }                        },  -); } toast.maketext (mainactivity. This,"Refresh", Toast.length_short). Show ();  Break;  CaseR.id.action_bar_search://your specific operationToast.maketext (mainactivity. This,"Search", Toast.length_short). Show ();  Break;  CaseR.id.action_bar_view://your specific operationToast.maketext (mainactivity. This,"View", Toast.length_short). Show ();  Break;    }            }        }); }}

Finally, note that in the Androidmanifest.xml file:

<Application Android:icon="@drawable/ic_launcher"Android:label="Testactionbar"Android:theme="@style/theme.greendroid"> <activity android:name=". Mainactivity"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity></application>

Do not add this sentence will be error, the Theme inherited from the parent= "Android:theme", there are some custom style. If you want to remove the Title column of window for example, you should add it in this theme file: <item name= "Android:windownotitle" >true</item>.
At last:

Android Custom Actionbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.