Android Custom title bar

Source: Internet
Author: User

Using the title bar in the project, it is found that the Actionbar provided by Android cannot be centered and the color of the title bar needs to be customized, so a custom title bar is required.

New Project MyTitle, do not modify 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:

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
9 android:layout_width= "Wrap_content"
Ten android:layout_height= "Wrap_content"
android:text= "This is the custom title bar"
android:textstyle= "Bold"
android:textcolor= "#FFFF0000"
/>
15
16
android:id= "@+id/button"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Point Me"
/>

</LinearLayout>

Create a new Titlestyle.xml file in the/res/values directory, define two style in it, one to change the size of the title bar, a background color to modify the title bar, as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>

3 <resources>
4
5 <style name= "Titlebackgroundcolor" >
6 <item name= "Android:background" > #FF0000FF </item>
7 </style>
8
9 <style name= "TitleStyle" parent= "Android:theme" >
Ten <item name= "Android:windowtitlesize" >40dip</item>
<item name= "Android:windowtitlebackgroundstyle" > @style/titlebackgroundcolor</item>
</style>
13
</resources>

Modify the Androidmanifest.xml file and add a line under the Application tab:

Android:theme= "@style/titlestyle"

Finally, modify the Mytitleactivity.java file, set the use of the custom title bar, implement button button monitoring, 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
public class Mytitleactivity extends Activity
11 {
The private Button Mbutton = null;
13
/** called when the activity is first created. */
@Override
-public void OnCreate (Bundle savedinstancestate)
17 {
Super.oncreate (savedinstancestate);
19//Use a custom title bar
Requestwindowfeature (Window.feature_custom_title);
Setcontentview (R.layout.main);
22//Use a layout file to define the title bar
GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.title);
24
Mbutton = (Button) This.findviewbyid (R.id.button);
26//Button Monitor
Mbutton.setonclicklistener (New View.onclicklistener ()
28 {
29
@Override
public void OnClick (View v)
32 {
/TODO auto-generated Method stub
Displaytoast ("clicked!");
35}
36});
37
38}
39
40//Show Toast function
$ private void Displaytoast (String s)
42 {
Toast.maketext (this, S, Toast.length_short). Show ();
44}
45
46}

Note that the order of the 20th to 23rd line of the above program cannot be scrambled.

Run the program:

Click on the "Point Me" button:

"Turn" http://www.cnblogs.com/lknlfy/archive/2012/03/08/2385724.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.