Preview the effect:
Material:
Create a new layout title_bar.xml with the following code:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colortitle"android:orientation= "Horizontal"> <ButtonAndroid:id= "@+id/title_back"Android:layout_width= "50DP"Android:layout_height= "50DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:background= "@drawable/firefox"android:textsize= "24DP" /> <TextViewAndroid:id= "@+id/title_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Title Text"Android:textcolor= "#fff"android:textsize= "24DP" /> <ButtonAndroid:id= "@+id/title_edit"Android:layout_width= "50DP"Android:layout_height= "50DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:background= "@drawable/chrome" /></LinearLayout>
Now that the title bar layout has been written, the rest is how to use the title bar in the program, and modify the code in the Activity_main.xml as follows:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <includeLayout= "@layout/title_bar" /></LinearLayout>
You just need to introduce the title layout through a single line of include statements:
<include layout= "@layout/title_bar"/>
Finally, don't forget to hide the system's own title bar in Mainactivity, as shown in the following code:
Public class extends activity{ @Override protectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (R.layout.title_bar); }}
In this way, no matter how many layouts you need to add a title bar, you just need a line of include statements.
Original address: http://www.cnblogs.com/woider/p/5119088.html
Android Custom title bar