Customize the layout of titlebar In the android title bar
The default activity title bar of the android program can only display a text segment, and cannot change its layout, color, and height. If you want to add an icon, button, input box, progress bar, or modify the color of the title bar, you can only use a custom title bar. You can add the following code to the custom title bar in the oncreate function. Note that the order of the Code must follow the following style; otherwise, the code will be invalid.
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_custom_title); (pay attention to the sequence)
Setcontentview (R. layout. mainactivity); // activity Layout
Getwindow (). setfeatureint (window. feature_custom_title, R. layout. titlebar); // layout of the title bar
The layout file is as follows:
R. layout. titlebar
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"horizontal"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
>
<
TextView
android:text
=
"@string/app_name"
android:textColor
=
"#000"
android:paddingRight
=
"3.0dip"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:text
=
"@string/battery_text"
android:textColor
=
"#000"
android:paddingRight
=
"3.0dip"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:id
=
"@+id/battery_text"
android:textColor
=
"#00f"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
</
LinearLayout
>
Although some controls can be added to the title bar, the height and background color of the title bar cannot be changed. To achieve this goal, you can only use theme (theme ). Therefore, add a style to the project. Change the background color to modify the value of Android: windowtitlebackgroundstyle, and change the title bar height to change the value of Android: windowtitlesize. The following is an example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "customwindowtitlebackground">
<Item name = "Android: background"> #778899 </item> // background color of the title bar
</Style>
<Style name = "activitytitlebar" parent = "Android: Theme">
<Item name = "Android: windowtitlesize"> 32dp </item> // height of the title bar
<Item name = "Android: windowtitlebackgroundstyle"> @ style/customwindowtitlebackground </item>
</Style>
</Resources>
Next, modify the androidmanifest. xml file, find the activity to customize the title bar, and add the Android: Theme value, for example:
<Activity Android: Name = ". mainactivity" Android: theme = "@ style/activitytitlebar">
Android: The theme value is the name value of a style defined in the above style. xml file. (Activitytitlebar above)
Follow these steps to modify the title bar layout, height, and background color.