Android Learning Essay-Customizing the title bar

Source: Internet
Author: User

In wirelessly do not like the system default title bar, then how to let custom a own title bar it.

The custom title bar is as follows:



First, you need to define a custom title bar layout Title.xml file (two images inside it is simple)

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "Horizontal" Android:layout_width= "Fill_parent" android:layout_height= "50DP" <!--the height of the new title bar--and Android:backgrou nd= "#f2f8f8" > <imagebutton android:id= "@+id/imagebutton1" android:layout_width= "40DP" Android        oid:layout_height= "40DP" android:layout_centervertical= "true" android:layout_alignparentleft= "true" android:layout_marginleft= "20DP" android:src= "@drawable/lbt"/> <textview android:l          Ayout_centerinparent= "true" android:layout_width= "Wrap_content" android:layout_height= "Match_parent"    android:text= "Custom title bar" android:textsize= "20sp" android:gravity= "center_vertical"/> <imagebutton android:id= "@+id/imagebutton1" android:layout_width= "40DP" android:layout_height= "40d P "Android:layout_centerinparent= "true" android:layout_alignparentright= "true" android:layout_marginright= "20DP" Andro   id:src= "@drawable/rbt"/> </RelativeLayout>

Then declare the use of the custom title bar in Mainactivity

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//declaration using a custom title bar requestwindowfeature (Window.feature_custom_title); Setcontentview ( R.layout.activity_main);//Use the custom title bar GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.title); }}

Then we'll set the height of the title bar and deal with Bugs that don't completely cover the original title bar on either side, so we need to define a style file Mystyle.xml

In the custom title bar, you often encounter a full effect that is not filled as follows:


Mystyle.xml

<resources xmlns:android= "http://schemas.android.com/apk/res/android" ><style name= "Titlebarstyle" Parent = "Android:theme" >     <item name= "android:windowtitlesize" >50dp</item><!--the height     of the original title bar-- <item name= "android:padding" >0dp</item><!--so that the new title bar extends completely to the sides of the original title bar--></style></ Resources>


And then what? Add a Theme property to the activity in Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.example.mytitle "android:versioncode=" 1 "android:versionname=" 1        .0 "> <uses-sdk android:minsdkversion=" 8 "android:targetsdkversion="/> <application         Android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name"             Android:theme= "@style/apptheme" > <activity android:name= "com.example.mytitle.MainActivity" Android:label= "@string/app_name" android:theme= "@style/titlebarstyle" > <intent-filter > <action android:name= "Android.intent.action.MAIN"/> <category android:name= "a Ndroid.intent.category.LAUNCHER "/> </intent-filter> </activity> </application>& Lt;/manifest> 


In the same way there are two places to add height settings to the title bar, one is the original title bar height, and the other is the height of the custom title bar. Generally set to two equals. Otherwise, the custom title bar only occupies the upper part of the original title bar. or only the upper part of the custom title bar is displayed.
The basic modification is done here.
Source: http://download.csdn.net/detail/liuhenghui5201/7644123

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.