I. Overview
The default title bar for each application (note the difference from the status bar) is only one line of text (the name of the new project), and color, size, etc. are fixed, giving the feeling of a more monotonous. But when it comes to landscaping, changing the title bar is one of those things, and although Android has defined a lot of style resources, more often than not, we need to use our own defined styles.
Second, the request
Modifies the program's title bar with its own defined style.
Third, realize
New project MyTitle, without modifying the Main.xml file, create a new layout file in the/res/layout directory title.xml, add a textview and a button inside, the complete Title.xml file is as follows:
The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Fill_parent" android:layout_height= "Match_parent" android:orientation= "Horizontal" >
<textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text= "It's a custom title bar." Android:textstyle= "Bold" Android:textcolor= "#FFFF0000" />
<button Android:id= "@+id/button" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text=, "dot me." />
</LinearLayout> |
Create a new Titlestyle.xml file in the/res/values directory, define two style inside, one to modify the title bar size, one to modify the title bar background color, as follows:
code is as follows |
copy code |
<?xml Version= "1.0" encoding= "Utf-8" <resources> <style name= "Titlebackgroundcolor" <item name= "Android:background" > #FF0000FF </item> </style> <style name= "TitleStyle" parent= "Android:theme" <item name= "Android:windowtitlesize" >40dip</item> <item name= "Android: Windowtitlebackgroundstyle "> @style/titlebackgroundcolor</item> </style </resources> |
Modify the Androidmanifest.xml file to add a row under the Application tab:
The code is as follows |
Copy Code |
Android:theme= "@style/titlestyle"
|
Finally, modify the Mytitleactivity.java file, set up to use the custom title bar, and implement the button button monitor, as follows:
The code is as follows |
Copy Code |
Package com.nan.title;
Import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.view.Window; Import Android.widget.Button; Import Android.widget.Toast;
public class Mytitleactivity extends activity { Private Button Mbutton = null;
/** called the activity is a. */ @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Use a custom title bar Requestwindowfeature (Window.feature_custom_title); Setcontentview (R.layout.main); Use a layout file to define a title bar GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.title);
Mbutton = (Button) This.findviewbyid (R.id.button); Button Monitor Mbutton.setonclicklistener (New View.onclicklistener () {
@Override public void OnClick (View v) { TODO auto-generated Method Stub Displaytoast ("clicked!"); } });
}
Show Toast function private void Displaytoast (String s) { Toast.maketext (this, S, Toast.length_short). Show (); }
} |
Note that the order of Line 20th to 23rd of the above procedure cannot be confused.
Run the program:
Click the "Click Me" button: