Android-yourself define the title bar

Source: Internet
Author: User

Android-yourself define the title bar


April 25, 2014 share knowledge points

Recently also more things, want to send a blog is the weak, this post mainly teach you how to achieve their own definition of the title bar, very easy. So smart you can read it in a moment.

Interested to be able to add the group number is 299402133, there is a wealth of learning resources, like-minded you. There will be some gains.

Implementation steps

* 1, give yourself a definition of the title to provide an interface

* 2. Apply your own definition title to the Activity form
* 3. Change the android system to the theme of the activity set by the default theme

:

Code Download: http://download.csdn.net/detail/wwj_748/7249585


/02_customtitle/res/layout/constom_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:background=" @drawable/rectangle " android:o rientation= "Horizontal" > <!--Specify the background. This background draws itself-- <textview style= "@android: Style/textappearance.medium" android:layout_width= "wrap _content " android:layout_height=" wrap_content " android:layout_gravity=" center_vertical " android: Text= "It_xiao witch" android:textcolor= "#ffffff" android:textsize= "14SP"/></linearlayout>



Here you use an image resource, which is under the Drawable folder:

/02_customtitle/res/drawable/rectangle.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    <!--define the fade-to-    <gradient        android:angle= "on"        Android : endcolor= "#80FF00FF"        android:startcolor= "#FFFF0000"/>    <!--defines the distance from the control's contents to the boundary (2 of the distance to four borders)--    <padding        android:bottom= "2DP"        android:left= "2DP"        android:right= "2DP"        android:top= "2DP"/>    <!--define rounded corners--    <corners android:radius= "8DP"/></shape>



/02_customtitle/src/com/wwj/constomtitle/mainactivity.java
Package Com.wwj.constomtitle;import Android.app.activity;import Android.os.bundle;import android.view.Window;/** * 1 , give yourself a definition of the title to provide an interface  * 2, to apply their own definition of the title to Activity form  * 3, the Android system for the activity set the default theme of the theme *  * @author WWJ *  */ public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Specify to use their own definition title requestwindowfeature (window.feature_custom_title); Setcontentview ( R.layout.activity_main);//Set the form's own definition title layout file GetWindow (). Setfeatureint (Window.feature_custom_title, R.layout.constom_title);}}

Change the default style

<!--The style inherits the default style of the system-    -<style name= "Customtheme" parent= "Android:theme" >        <!--setting the title foreground color is transparent-- >        <item name= "Android:windowcontentoverlay" > @drawable/nocolor</item>        <!--set header height to 44DP- -        <item name= "android:windowtitlesize" >44dp</item>        <!--set title background color-        <item Name= "Android:windowtitlebackgroundstyle" > @style/custombg</item>    </style>    <!-- Define a background style--    <style name= "CUSTOMBG" >        <item name= "Android:background" > @drawable/rectangle </item>    </style>


/02_customtitle/res/values/drawable.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <!--define a transparent color--    <drawable name= " Nocolor "> #00000000 </drawable></resources>


Setting Themes in Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? 

><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.wwj.constomtitle" Android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" Android: targetsdkversion= "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launche R "android:label=" @string/app_name "android:theme=" @style/customtheme "> <activity A Ndroid:name= "com.wwj.constomtitle.MainActivity" android:label= "@string/app_name" > <intent-fil ter> <action android:name= "Android.intent.action.MAIN"/> <category android:name = "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </APPLICATION&G T;</manifest>










Android-yourself define the 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.