1. Create a layout file, such as Mycustomtitle.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "45DP"Android:background= "@color/blue" > <ImageButton Android:id= "@+id/share"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:layout_alignparentright= "true"android:src= "@drawable/share"Android:background= "@color/blue"android:contentdescription= "@string/share"Android:paddingright= "5DP"/> <ImageButton Android:id= "@+id/back"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"android:contentdescription= "@string/back"android:src= "@drawable/back"Android:background= "@color/blue"android:padding= "5DP"/> <TextView android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"android:gravity= "Center"Android:text= "@string/title"Android:textcolor= "#fff"android:textsize= "20SP"Android:textstyle= "Bold"/></relativelayout>
Effect
2. Call the caption method in Java, as follows: Note the order of the Code, it is important
protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Requestwindowfeature (window.feature_custom_title); Setcontentview (r.layout.activity); GetWindow (). Setfeatureint (Window.feature_custom_title,r.layout.mycustomtitle); }
Android Ui:layout Custom title bar