Android's Custom title

Source: Internet
Author: User

We know that every activity we create, the system gives us a black title by default, and I'll take a look at how to implement a custom heading style in this article. Compared to the system for us to provide the style, the custom title can satisfy our idealism of the custom design, so that our interface looks more high-end, in order to better attract the user's use. Here's what to start today:

1, since is the custom title style, first we need to design a custom title layout, through this layout file, we can arbitrarily design our heading style (Title.xml):

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#aa0000"Android:text= "This is my custom title"/> <Button Android:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "More"/></linearlayout>

2, write a good layout file, the following we began to design the title of the style, Project res directory under Styles.xml:

<resources>    <style name= "Itcasttheme" parent= "Android:theme" >         <item name= "Android: Windowcontentoverlay "> @color/nonecolor</item>         <item name=" Android:windowtitlesize ">44dp</ item><!--Set the width of the custom caption--         <item name= "Android:windowtitlebackgroundstyle" > @style/itcastbg</ item><!--Custom Heading styles-    </style>        <style name= "ITCASTBG" >        <item name= "Android: Background ">@drawable/rectangle</item>    </style></resources>

3, the red font part, is I through the drawable file rectangle.xml file implementation of a title background:

<?xml version= "1.0" encoding= "utf-8"?><shape     xmlns:android= "http://schemas.android.com/apk /res/android "    android:shape=" Rectangle ">    <gradient        android:angle=" + "        Android:endcolor= "#1DC9CD"        android:startcolor= "#A2E0FB"/>    <padding        android:left= "2DP"        android:top= "2DP"        android:right= "2DP"         android:bottom= "2DP"/></shape>

4. Here I can begin to modify our main activity:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        requestwindowfeature (window.feature_custom_title);                Setcontentview (R.layout.activity_main); GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.title);//Set our custom title Button MyButton=(Button) Findviewbyid (R.id.button); Mybutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "Please look forward to", Toast.length_short). Show ();    }        }); }}

Note that the red part must be written before the layout file is referenced, or the effect will not be reached.

5. Finally, we need to set the style for our activity in the Androidmanifest.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Cn.edu.hpu.activity_title"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Application Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name" > <Activity Android:name=". Mainactivity "android:theme= "@style/itcasttheme"> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </acti Vity> </application></manifest>

OK, the introduction of the Android custom title is finished, interested children shoes can be realized. Novice learning, Master communication.

Android's Custom title

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.