Android自定义ActionBar背景色、字体颜色等样式style

【背景】

Android中想要去自定义ActionBar的背景色等样式。

【折腾过程】

1.自己找代码,发现对应的配置的地方了:

AndroidManifest.xml

?
1
2
< application
     android:theme = "@style/AppTheme"  >

找到

/res/values/styles.xml

中的:

?
1
2
3
< style  name = "AppTheme"  parent = "@android:style/Theme.Holo.Light" >
     < item name = "android:actionModeBackground" >@drawable/cab_background_top_xxxstyle</ item >
</ style >

对应的cab_background_top_xxxstyle是个图片,是深蓝色的,所以效果是:

actionbar dark blue effect by png drawable

2.而此处,由于背景色不是通过的Color去定义的,所以无法很简单的通过直接改color而达到改变背景色的效果。

所以只能去考虑换一个这个drawable的png图片。

但是发现该图片时那种9 pitch的,但是自己目前不太懂,不会生成,所以要去研究那个:

【记录】研究Android中的9 patch的图片的原理以及如何去生成对应图片

3.然后看到:

http://jgilfelt.github.io/android-actionbarstylegenerator/

中有帮忙生成actionbar的9patch的图片的,所以去试了试:

Android Action Bar Style Generator effect 1

Android Action Bar Style Generator effect 2

Android Action Bar Style Generator effect 3

Android Action Bar Style Generator effect 4

然后下载到:

actionbar_style_rocksensor.zip

解压后,得到很多对应的文件:

generated actionbar style for rocksensor

 

然后去把所有文件都拷贝到项目中,去试试效果:

newly style using generated 9patch files

效果还不错。

 

【总结】

以后如果想要换整体的ActionBar等主题的话,就可以去利用:

Android Action Bar Style Generator


如果上述方法嫌麻烦的话,这里给出一种简单的。

Activity会自动使用style.xml文件中定义的样式,只要对这些样式略作修改(继承需要的主题,重写特定属性)就可以达成比较简单的定制。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <!--  
  4.         Base application theme for API 14+. This theme completely replaces  
  5.         AppBaseTheme from BOTH res/values/styles.xml and  
  6.         res/values-v11/styles.xml on API 14+ devices.  
  7.     -->  
  8.     <style name="AppBaseTheme" parent="android:Theme.Holo.Light">  
  9.         <!-- API 14 theme customizations can go here. -->  
  10.         <item name="android:actionBarStyle">@style/my_actionbar_style</item>   
  11.     </style>  
  12.       
  13.     <style name="my_actionbar_style" parent="@android:style/Widget.Holo.Light.ActionBar">  
  14.         <item name="android:background">#647b97</item>  
  15.         <item name="android:titleTextStyle">@style/AcBar_titleStyle</item>    
  16.     </style>  
  17.     <style name="AcBar_titleStyle">    
  18.         <item name="android:textSize">18sp</item>  
  19.         <item name="android:textColor">#FFFFFF</item>    
  20.     </style>  
  21.    
  22. </resources>  

android:Widget.ActionBar是系统自定义的样式,包含的内容比较多。现在对这个样式中的背景色和标题色进行修改,android:background影响背景颜色,android:titleTextStyle影响标题的样式(因为有多项,所以要再引用一个自定义的样式)。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值