In Android Actionbar, the Actionbar view is fixed, the left side is the program's icon and title, the right side is the added MenuItem, if you want to customize the view in the Actionbar will be custom views.
The first thing to do is to define Actionbar XML
<?xml version="1.0"encoding="Utf-8"? ><framelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="fill_parent"Android:id="@+id/layout01"> <imagebutton android:id="@+id/left"Android:layout_width="wrap_content"Android:layout_height="fill_parent"Android:background="@android: Color/transparent"android:src="@drawable/show_left"android:layout_gravity="left|center_vertical"Android:paddingleft="8dip"android:clickable="true"/> <textview android:id="@+id/tv"Android:background="@android: Color/transparent"Android:layout_width="wrap_content"Android:layout_height="match_parent"Android:text="Home Page"Android:textcolor="@android: Color/white"android:textsize="18dip"Android:paddingtop="2dip"android:layout_gravity="Center"/> <imagebutton android:id="@+id/more"Android:layout_width="wrap_content"Android:layout_height="fill_parent"Android:background="@android: Color/transparent"android:src="@drawable/cross"Android:paddingright="8dip"android:clickable="true"android:layout_gravity="right|center_vertical"/> </FrameLayout>
Then set the ActionBar style in Oncreatemenuitem, be sure to set Actionbar.setdisplayoptions (Actionbar.display_show_custom) and Actionbar.setdisplayshowcustomenabled (TRUE);
Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); ActionBar= This. Getactionbar (); Actionbar.layoutparamsparams=NewActionbar.layoutparams (Layoutparams.match_parent,layoutparams.match_parent,gravity.center); View View=layoutinflater. from( This). Inflate (R.layout.actionbar,NULL); Actionbar.setcustomview (View,params); Actionbar.setdisplayoptions (Actionbar.display_show_custom); Actionbar.setdisplayshowcustomenabled (true); ImageButton More= (ImageButton) This. Findviewbyid (R.id.more); ImageButton Left= (ImageButton) This. Findviewbyid (R.id.left); Left.setontouchlistener (NewMytouchlistener ()); More.setontouchlistener (NewMytouchlistener ()); return true; }}classMytouchlistener implements ontouchlistener{@Override PublicBoolean OnTouch (View view, Motionevent arg1) {if(arg1.getaction () = =Motionevent.action_down) {View.setbackgroundcolor (Color.ltgray); } if(arg1.getaction () = =motionevent.action_up) {View.setbackgroundcolor (color.transparent); } return true; }
Android Actionbar title bar