Customize the layout of titlebar In the android title bar

Source: Internet
Author: User

Customize the layout of titlebar In the android title bar

The default activity title bar of the android program can only display a text segment, and cannot change its layout, color, and height. If you want to add an icon, button, input box, progress bar, or modify the color of the title bar, you can only use a custom title bar. You can add the following code to the custom title bar in the oncreate function. Note that the order of the Code must follow the following style; otherwise, the code will be invalid.

Super. oncreate (savedinstancestate );

Requestwindowfeature (window. feature_custom_title); (pay attention to the sequence)

Setcontentview (R. layout. mainactivity); // activity Layout
Getwindow (). setfeatureint (window. feature_custom_title, R. layout. titlebar); // layout of the title bar

The layout file is as follows:

R. layout. titlebar

 

<? xml
version = "1.0"
encoding = "utf-8" ?> < LinearLayout          xmlns:android = "http://schemas.android.com/apk/res/android"          android:orientation = "horizontal"          android:layout_width = "fill_parent"          android:layout_height = "wrap_content" >          < TextView                  android:text = "@string/app_name"                  android:textColor = "#000"                  android:paddingRight = "3.0dip"                  android:layout_width = "wrap_content"                  android:layout_height = "wrap_content" />          < TextView                  android:text = "@string/battery_text"                  android:textColor = "#000"                  android:paddingRight = "3.0dip"                  android:layout_width = "wrap_content"                  android:layout_height = "wrap_content" />          < TextView                  android:id = "@+id/battery_text"                  android:textColor = "#00f"                  android:layout_width = "wrap_content"                  android:layout_height = "wrap_content" /> </ LinearLayout >

Although some controls can be added to the title bar, the height and background color of the title bar cannot be changed. To achieve this goal, you can only use theme (theme ). Therefore, add a style to the project. Change the background color to modify the value of Android: windowtitlebackgroundstyle, and change the title bar height to change the value of Android: windowtitlesize. The following is an example:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "customwindowtitlebackground">
<Item name = "Android: background"> #778899 </item> // background color of the title bar
</Style>
<Style name = "activitytitlebar" parent = "Android: Theme">
<Item name = "Android: windowtitlesize"> 32dp </item> // height of the title bar
<Item name = "Android: windowtitlebackgroundstyle"> @ style/customwindowtitlebackground </item>
</Style>
</Resources>

Next, modify the androidmanifest. xml file, find the activity to customize the title bar, and add the Android: Theme value, for example:

<Activity Android: Name = ". mainactivity" Android: theme = "@ style/activitytitlebar">

Android: The theme value is the name value of a style defined in the above style. xml file. (Activitytitlebar above)

Follow these steps to modify the title bar layout, height, and background color.

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.