Android Custom title bar

Source: Internet
Author: User

Preview the effect:

Material:

Create a new layout title_bar.xml with the following code:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colortitle"android:orientation= "Horizontal">    <ButtonAndroid:id= "@+id/title_back"Android:layout_width= "50DP"Android:layout_height= "50DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:background= "@drawable/firefox"android:textsize= "24DP" />    <TextViewAndroid:id= "@+id/title_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Title Text"Android:textcolor= "#fff"android:textsize= "24DP" />    <ButtonAndroid:id= "@+id/title_edit"Android:layout_width= "50DP"Android:layout_height= "50DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:background= "@drawable/chrome" /></LinearLayout>

Now that the title bar layout has been written, the rest is how to use the title bar in the program, and modify the code in the Activity_main.xml as follows:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <includeLayout= "@layout/title_bar" /></LinearLayout>

You just need to introduce the title layout through a single line of include statements:

<include layout= "@layout/title_bar"/>

Finally, don't forget to hide the system's own title bar in Mainactivity, as shown in the following code:

 Public class extends activity{    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (R.layout.title_bar);    }}

In this way, no matter how many layouts you need to add a title bar, you just need a line of include statements.

Original address: http://www.cnblogs.com/woider/p/5119088.html

Android Custom title bar

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.