【背景】

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

【折腾过程】

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

AndroidManifest.xml


​​<​​​        ​​application​​       


​​android:theme​​​ ​​=​​​ ​​"@style/AppTheme"​​​ ​​>​​


找到

/res/values/styles.xml

中的:



​​<​​​        ​​style​​​         ​​name​​​        ​​=​​​        ​​"AppTheme"​​​         ​​parent​​​        ​​=​​​        ​​"@android:style/Theme.Holo.Light"​​​        ​​>​​       


​​<​​​ ​​item​​​ ​​name​​​ ​​=​​​ ​​"android:actionModeBackground"​​​ ​​>@drawable/cab_background_top_xxxstyle</​​​ ​​item​​​ ​​>​​


​​</​​​ ​​style​​​ ​​>​​


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

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


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

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

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

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

3.然后看到:

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

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

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


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


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


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


然后下载到:

actionbar_style_rocksensor.zip

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

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


 

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

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


效果还不错。

 

【总结】

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

​Android Action Bar Style Generator​

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

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


<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/my_actionbar_style</item>
</style>

<style name="my_actionbar_style" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#647b97</item>
<item name="android:titleTextStyle">@style/AcBar_titleStyle</item>
</style>
<style name="AcBar_titleStyle">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>

</resources>


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