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):
<?xmlversion= "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=" 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" > <itemname= "android:windowcontentover Lay "> @color/nonecolor</item> <itemname=" android:windowtitlesize ">44dp</item><!-- Sets the width of the custom caption-<itemname= "Android:windowtitlebackgroundstyle" > @style/itcastbg</item><!-- Custom caption Styles-</style> <style name= "ITCASTBG" > <itemname= "Android:background" > @drawable/R Ectangle</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:st Artcolor= "#A2E0FB"/> <padding android:left= "2DP" android:top= "2DP" android:right= "2DP" an Droid: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 custom title button mybutton = (Button) findViewById ( R.id.button); mybutton.setonclicklistener (New OnClickListener () { @Override &nbsP;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"? ><manifestxmlns: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> & NBsP; <actionandroid:name= " Android.intent.action.MAIN " /> <categoryandroid: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