Android Phone Development Custom title bar

Source: Internet
Author: User
Tags xmlns

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

<?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."

/>

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:

The code is as follows

<?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

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

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:

Related Article

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.