android開發之定製標題欄 --- 附源碼


http://aswang.iteye.com/blog/1483904

在開發上個應用 話費速查 的時候,需要修改標題欄的樣式,但是android自身的標題欄是不支持修改樣式的,因此需要通過下面的方式讓android支持自定義標題欄:

 

Java代碼  收藏代碼
  1. super.onCreate(savedInstanceState);  
  2. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  3. setContentView(R.layout.main);  
  4. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);  

 

 注意上面的代碼順序不要改變,否則不會有效果。

 

上面的R.layout.title就是自定義標題欄的佈局文件:

 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:layout_width="fill_parent"   
  4.         android:layout_height="wrap_content"  
  5.         android:orientation="horizontal"  
  6.         android:layout_gravity="center_vertical">  
  7.         <ImageView  
  8.             android:src="@drawable/phone"  
  9.             android:contentDescription="@string/desc"    
  10.             android:gravity="center_vertical"  
  11.             android:layout_width="wrap_content"  
  12.             android:layout_height="fill_parent"/>  
  13.         <TextView android:text="@string/app_name"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="fill_parent"   
  16.             android:layout_marginLeft="5dip"  
  17.             android:gravity="center_vertical"  
  18.             android:textColor="#FFFFFF"  
  19.             android:textSize="20sp"/>  
  20.         <TextView android:text="@string/use_tips"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="fill_parent"   
  23.             android:layout_marginLeft="10dip"  
  24.             android:gravity="center_vertical"  
  25.             android:textColor="#ABACAC"  
  26.             android:textSize="15sp"/>  
  27. </LinearLayout>  

 

但是僅僅這樣,還是不夠的。標題欄的背景顏色和高度還是不能改變,還需要給它定義樣式:

 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:android ="http://schemas.android.com/apk/res/android">  
  3.     <style name ="CustomWindowTitleBackground">  
  4.           <item name ="android:background">#1F497D</item>  
  5.     </style>  
  6.     <style name="title"  parent="android:Theme">   
  7.         <item name ="android:windowTitleSize">45dp</item>   
  8.         <item name ="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>   
  9.   </style>   
  10. </resources>  

  上面樣式文件中的背景顏色 和 標題大小都是可以自己修改的。

 

最後,將該樣式應用到Main Activity(即啓動Activity):

 

 

Xml代碼  收藏代碼
  1. <activity  
  2.             android:name=".BillQueryActivity"  
  3.             android:label="@string/app_name"   
  4.             android:theme="@style/title">  
  5.             <intent-filter>  
  6.                 <action android:name="android.intent.action.MAIN" />  
  7.                 <category android:name="android.intent.category.LAUNCHER" />  
  8.             </intent-filter>  
  9.         </activity>  

 

好了,現在可以看看效果了:


 

 

源碼下載地址:點擊這裏


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章