I. Overview
The default title bar of each application (note the difference with the status bar) has only one line of text (the name of the project when the project is created), and the color and size are fixed, which makes people feel monotonous. However, when the program needs to be beautified, modifying the title bar is one of the items. Although android has defined many style resources, we need to use our own styles more often.
Ii. Requirements
Use a custom style to modify the title bar of a program.
Iii. Implementation
Create a project mytitle without modifying main. XML file. Create a layout file title in the/RES/layout directory. XML, add a textview and a button in it, complete title. the XML file is as follows:
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "match_parent"
5 Android: Orientation = "horizontal"
6>
7
8 <textview
9 Android: layout_width = "wrap_content"
10 Android: layout_height = "wrap_content"
11 Android: text = "this is a customized title bar"
12 Android: textstyle = "bold"
13 Android: textcolor = "# ffff0000"
14/>
15
16 <button
17 Android: Id = "@ + ID/button"
18 Android: layout_width = "wrap_content"
19 Android: layout_height = "wrap_content"
20 Android: text = "Click me"
21/>
22
23 </linearlayout>
Create the titlestyle. xml file in the/RES/values directory, and define two styles, one for modifying the title bar size and the other for modifying the background color of the title bar, as shown below:
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <resources>
4
5 <style name="TitleBackgroundColor">
6 <item name="android:background">#FF0000FF</item>
7 </style>
8
9 <style name="titlestyle" parent="android:Theme" >
10 <item name="android:windowTitleSize">40dip</item>
11 <item name="android:windowTitleBackgroundStyle">@style/TitleBackgroundColor</item>
12 </style>
13
14 </resources>
Modify the androidmanifest. xml file and add a line under the application Tag:
android:theme="@style/titlestyle"
Finally, modify the mytitleactivity. Java file and set the custom title bar to monitor the button as follows:
1 package com. Nan. title;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 import Android. View. view;
6 Import Android. View. window;
7 Import Android. widget. Button;
8 Import Android. widget. Toast;
9
10 public class mytitleactivity extends Activity
11 {
12 private button mbutton = NULL;
13
14/** called when the activity is first created .*/
15 @ override
16 public void oncreate (bundle savedinstancestate)
17 {
18 super. oncreate (savedinstancestate );
19 // use the custom title bar
20 requestwindowfeature (window. feature_custom_title );
21 setcontentview (R. layout. Main );
22 // use the layout file to define the title bar
23 getwindow (). setfeatureint (window. feature_custom_title, R. layout. Title );
24
25 mbutton = (button) This. findviewbyid (R. Id. Button );
26 // button listening
27 mbutton. setonclicklistener (New View. onclicklistener ()
28 {
29
30 @ override
31 public void onclick (view V)
32 {
33 // todo auto-generated method stub
34 displaytoast ("clicked! ");
35}
36 });
37
38}
39
40 // display the toast Function
41 private void displaytoast (string S)
42 {
43 toast. maketext (this, S, Toast. length_short). Show ();
44}
45
46}
Pay attention to the 20th ~ The Order of 23 rows cannot be disordered.
Run the program:
Click "Click me: