Share: Android's custom title

Source: Internet
Author: User

we know that every single one we create Activity, the system defaults to provide us with a black title, this article I will lead you to touch on how to implement a custom heading style. 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 the layout file, the following we began to design the title of the style, project res directory Styles.xml:

<resources> <style name= "Itcasttheme" parent= "Android:theme" > <item name= "android:windowcontent Overlay "> @color/nonecolor</item> <item name=" android:windowtitlesize ">44dp</item><!--set from Defines the width of the caption-<item name= "Android:windowtitlebackgroundstyle" > @style/itcastbg</item>< the style of the custom caption -</style> <style name= "ITCASTBG" > <item name= "android:background" > @drawable/rectan Gle</item> </style> </resources>

3 drawable file under 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 class mainactivity extends activity {      @Override     protected void oncreate (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 own custom title          button  mybutton =  (Button) Findviewbyid (R.id.button);         mybutton.setonclicklistener (New onclicklistener ()  {             @Override            public void onclick ( VIEW&NBSP;V)  {                 toast.maketext (mainactivity.this,  "Please expect",  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>         </activity>    </application> </ Manifest>

all right, about Android Custom Title Introduction is finished, of course, the development of the APP is also required to conduct a full range of testing:www.ineice.com


Share: 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.