The first step is to implement a custom title bar that needs to be written in the OnCreate method
Requestwindowfeature (Window.feature_custom_title);
Setcontentview (R.layout.main);
GetWindow (). Setfeatureint (Window.feature_custom_title, R.layout.title_bar);
Attention:
Requestwindowfeature to Setcontentview before the
GetWindow () Setfeatureinit preferably after Setcontentview.
The second step is to write your own layout files, to achieve the custom title bar.
However, we will encounter some problems, that is, the height of the title bar can not be customized ~ The following is the solution ~
Here's an example to see how to do this.
1, under the layout to create a titlebtn.xml file, the contents are as follows:
The code is as follows |
Copy Code |
XML version= "1.0" encoding= "Utf-8"?> <<>relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:orientation= "Horizontal" > <<>imagebutton Android:id= "@+id/imagebutton1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentleft= "true" Android:layout_centervertical= "true" Android:background= "#00000000" android:src= "@drawable/prv"/> <<>textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_centerinparent= "true" android:text= "title bar"/> <<>imagebutton Android:id= "@+id/imagebutton1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentright= "true" Android:layout_centerinparent= "true" Android:background= "#00000000" android:src= "@drawable/next"/> <RelativeLayout> |
modifying Style.xml files
Add the following code
The code is as follows |
Copy Code |
Name= "Customwindowtitlebackground" > Name= "Android:background" > #00cc00 Name= "Test" parent= "Android:theme" > Name= "Android:windowtitlesize" >50DP Name= "Android:windowtitlebackgroundstyle" > @style/customwindowtitlebackground |
Add to Androidmanifest
code is as follows |
copy code |
Android : Name= ". Customtitilebaractivity " android:label=" @string/app_name "android:theme=" @style/test " Android:name=" Android.intent.action.MAIN "/> android:name=" Android.intent.category.LAUNCHER "/> |