Custom title bar for android mobile development

Source: Internet
Author: User

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:

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 = "this is a customized 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 = "Click me"

/>

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:

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 and add a line under the application Tag:

The Code is as follows:

Android: theme = "@ style/titlestyle"

Finally, modify the MyTitleActivity. java file and set the custom title bar to monitor the Button 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 when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState)

{

Super. onCreate (savedInstanceState );

// Use the custom title bar

RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );

SetContentView (R. layout. main );

// Use the layout file to define the title bar

GetWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. title );

MButton = (Button) this. findViewById (R. id. button );

// Button listening

MButton. setOnClickListener (new View. OnClickListener ()

{

@ Override

Public void onClick (View v)

{

// TODO Auto-generated method stub

DisplayToast ("Clicked! ");

}

});

}

// Display the Toast Function

Private void displayToast (String s)

{

Toast. makeText (this, s, Toast. LENGTH_SHORT). show ();

}

}

Pay attention to the 20th ~ The Order of 23 rows cannot be disordered.

Run the program:

Click "Click me:

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.