The example of this article describes the implementation of the Android custom Actionbar. Share to everyone for your reference. The implementation methods are as follows:
Android 3.0 and above already have Actionbar APIs that can be introduced through the introduction of support Package references these APIs in platforms below 3.0, but here, completely customizing a actionbar without introducing additional jar packs, referencing the Open-source UI component Greeendroid, Project home: https://github.com/ Cyrilmottier/greendroid. To extract relevant documents about Actionbar, you can put them in your own project and finally download the address. 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:
Copy Code code 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, first to define a namespace, the name is arbitrary, such as the bar above. Title is the caption on the Actionbar; type is the layout of the item on the left side of the Actionbar, there are three types, one is normal: the left is a display of the home page of the ImageButton and display title TextView, One is dashboard, the left is a display of the app icon ImageView and display title TextView, one is empty, the left only one display title TextView, the right side are added buttons, the default is normal. as follows: corresponding to normal, dashboard and empty respectively.
、、
There are also some properties, homedrawable can replace the picture on the left, the default is the picture of the 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.
Copy Code code as follows:
public class Mainactivity extends activity {
Private Actionbar Mactionbar;
Private final Handler Mhandler = new Handler ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
For the case where type is normal
Mactionbar = (Actionbar) Findviewbyid (R.id.gd_action_bar);
Add the specific item on the right, it's actually ImageButton
It encapsulates some type within itself, such as refresh, search, mainly by directly using some of its built-in images as ImageButton src
followed by the ID of the ImageButton, which can be defined in Values/ids.xml
Refreshing is different from other type, except for a ImageButton, there is a ProgressBar, below you can see
Mactionbar.additem (Type.refresh, R.id.action_bar_refresh);
Mactionbar.additem (Type.search, R.id.action_bar_search);
You can also set your own picture by adding an item yourself as follows
Mactionbar.additem (
Mactionbar.newactionbaritem (Normalactionbaritem.class)
. setdrawable (R.drawable.gd_action_bar_eye)
. Setcontentdescription ("View"), R.id.action_bar_view);
Add a listener event to item that is ImageButton
Mactionbar.setonactionbarlistener (New Onactionbarlistener () {
@Override
public void onactionbaritemclicked (int position) {
if (position = = ActionBar.OnActionBarListener.HOME_ITEM) {
The action that is triggered when you press the home button on the left
Toast.maketext (Mainactivity.this, "home or Back",
Toast.length_short). Show ();
Return
}
Final Actionbaritem item = mactionbar.getitem (position);
Switch (Item.getitemid ()) {
Case R.id.action_bar_refresh:
if (item instanceof Loaderactionbaritem) {
Mhandler.postdelayed (New Runnable () {
@Override
public void Run () {
This method can be used to show and hide the ProgressBar.
((Loaderactionbaritem) item). setloading (false);
}
}, 2000);
}
Toast.maketext (Mainactivity.this, "Refresh",
Toast.length_short). Show ();
Break
Case R.id.action_bar_search:
Your specific operation.
Toast.maketext (mainactivity.this, "search",
Toast.length_short). Show ();
Break
Case R.id.action_bar_view:
Your specific operation.
Toast.maketext (Mainactivity.this, "view",
Toast.length_short). Show ();
Break
}
}
});
}
}
Finally, note that in the Androidmanifest.xml file:
Copy Code code as follows:
<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 an error, this Theme inherited from Parent= "Android:theme", which has some custom style. If you want to remove the title bar of the window, add it to the theme file: <item name= "Android:windownotitle" >true</item>.
Final Effect Chart:
Project instance code click here to download the site.
I hope this article will help you with your Android program.