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>  

 

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


 

 

源码下载地址:点击这里


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值