自定义一个layout,然后通过requestWindowFeature和getWindow().setFeatureInt方法调用,

    但是存在填充不满的问题,而且比较麻烦。

 

但是,我只是想改变标题栏字体的大小、颜色,并设置一个背景图片,并不想定义一个layout的xml文件。

那么,android有没有提供解决上述问题的方法呢,答案是肯定的,一定可以。

默认的标题栏式样太难看了,灰不啦吉的。

 

后来参考了一篇洋人博友的文章,最终有所收获。

http://zaman91.wordpress.com/2010/03/18/android-how-to-customize-default-themes-and-styles/

 

具体的操作步骤是:

1、在res/values添加styles.xml文件。

     .在eclipse中,选择File -> New -> Other菜单

     .在弹出窗口中,选择Android/Android XML File,点击 Next

     .在添加XML窗口中,输入文件名"styles.xml",选中Values,

       输入文件夹路径"/res/values"

     .然后点击 Finish

 

2、在res/values添加themes.xml文件。

    操作方法同步骤1,只是文件名输入"themes.xml"

 

3、styles.xml文件的代码

   

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:adnroid="http://schemas.android.com/apk/res/android">  
  3.   
  4. <style name="CustomWindowTitleBackground" parent="android:WindowTitleBackground">  
  5.   <item name="android:background">@drawable/bgitem>  
  6. style>  
  7.   
  8. <style name="CustomWindowTitle" parent="android:WindowTitle">  
  9.   <item name="android:textAppearance">@style/CustomWindowTitleTextitem>  
  10. style>  
  11.   
  12. <style name="CustomWindowTitleText" parent="android:TextAppearance.WindowTitle">  
  13.   <item name="android:textColor">#00fitem>  
  14.   <item name="android:textSize">14spitem>  
  15.   <item name="android:textStyle">bolditem>  
  16. style>  
  17.   
  18. resources>  

 

4、themes.xml文件的代码

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:adnroid="http://schemas.android.com/apk/res/android">  
  3.   
  4. <style name="titleTheme" parent="android:Theme" >  
  5.     
  6.   <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackgrounditem>  
  7.   <item name="android:windowTitleStyle">@style/CustomWindowTitleitem>  
  8. style>  
  9.   
  10. resources>  

 

5、修改工程的Manifest.xml文件

     .在Activity中加入android:theme="@style/titleTheme"即可

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.android.customtitle"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".CustomTitle"  
  8.                   android:label="@string/app_name"  
  9.                   android:theme="@style/titleTheme">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             intent-filter>  
  14.         activity>  
  15.   
  16.     application>  
  17.     <uses-sdk android:minSdkVersion="9" />  
  18.   
  19. manifest>   

 

6、效果图

 

说明:

    .关于@drawable/bg,它是title的背景图. bg.png格式。

      直接在res目录下创建修文件夹drawable及文件bg.png即可。

    .个人感觉,android的所有style和theme都是可以继承并且修改的。

 

关于style和theme的详细,可以查看一下的参考资料。

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/values/styles.xml

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/values/themes.xml

更多相关文章

  1. ArcGIS API for Android(安卓)案例教程 4
  2. Android(安卓)Resource的使用和介绍
  3. [置顶] Eclipse工程转换为Android(安卓)Studio工程
  4. 2011年Android(安卓)Binder机制学习笔记之一
  5. android 数据存储之 读写文件
  6. 初识 Android(安卓)Native Development Kit (NDK)
  7. Android(安卓)M 差分包的制作流程
  8. Android(安卓)8.1 开机流程分析(1)
  9. 关于用百度地图开发的准备工作

随机推荐

  1. 用Flask+Aiohttp+Redis维护动态代理池
  2. App抓包其实没那么复杂!Charles来帮你搞定
  3. 在同一基准下对前端框架进行比较[每日前
  4. Scrapy对接Docker
  5. 付费代理的使用
  6. Android金额输入框只允许输入小数点后两
  7. 为什么你不应该成为一个“数据科学“通才
  8. 内网主机从外面连接不了?SSH反向隧道来帮
  9. Scrapy框架的使用之Spider的用法
  10. Scrapy框架的使用之Scrapy入门