"Android UI" custom title bar with buttons

Source: Internet
Author: User

Custom title bars are common in many Android apps and can be described as a useful UI design approach. Self -

has also the spirit of learning attitude, after a variety of pits, finally realized, and now summarized as follows:

A: Approximate process

1. Set a custom theme style for the specified Android activity, where the custom theme style is key

If you use Theme.holo or Theme.light in Android 4.0 or later, the program will

Keep reporting Errors-youcannot combine custom title with other feature titles

2. Add code to the corresponding activity

        Super. OnCreate (savedinstancestate);        Requestwindowfeature (window.feature_custom_title);        Setcontentview (r.layout.main);        GetWindow (). Setfeatureint (window.feature_custom_title,r.layout.mycustomtitle);

3. Use the following custom theme in Styles.xml to find out that only the default theme is used for the first step

Wrong, it's all kinds of pits!

<Resources>    <stylename= "Windowtitlebackground" >                    </style>    <stylename= "MyTheme"Parent= "Android:theme">        <Itemname= "Android:windowtitlesize">60dp</Item>        <Itemname= "Android:windowtitlebackgroundstyle">@style/windowtitlebackground</Item>                    </style></Resources>

4. Key techniques, use relativelayout to align the components of the custom title

Two: Test mainactivity source Code

 PackageCom.gloomyfish.titledemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Window; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_custom_title);        Setcontentview (R.layout.main);    GetWindow (). Setfeatureint (Window.feature_custom_title,r.layout.mycustomtitle); }    }

Three: XML resource file

Mycustomtitle.xml's Content

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"android:layout_gravity= "Fill_horizontal"android:orientation= "Horizontal"Android:layout_height= "Fill_parent" >    <ButtonAndroid:id= "@+id/header_left_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_marginleft= "5DP"android:layout_centervertical= "true"Android:text= "Back"Android:textcolor= "#000000"/>            <TextViewAndroid:id= "@+id/header_text"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_torightof= "@+id/header_left_btn"Android:layout_toleftof= "@+id/header_right_btn"Android:text= "My Title Bar"android:textsize= "20SP"Android:textstyle= "Bold"Android:textcolor= "#FFFFFF"android:gravity= "Center"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:singleline= "true" />                   <ButtonAndroid:id= "@+id/header_right_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:layout_marginright= "5DP"android:layout_centervertical= "true"Android:text= "Next"Android:textcolor= "#000000"/></Relativelayout>

Finally, don't forget to add a custom theme to the manifest configuration file in Androi

"Android UI" custom title bar with buttons

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.