对于android项目,一般都需要自定义theme,好使整个android项目有自己独特的风格,不过现实中很多android项目theme和style都能够以命名都很混乱
为了保证清楚一般可以采取以后几个步骤:
1.style和theme分开为两个文件。
一般需要在values下面有两个文件,一个是styles.xml,themes.xml, 有个甚至还需要好几个,千万不要把style和theme写在一个文件里面,久而久之,那里面就混乱的不成样子了。
2.有个总的theme和他的basetheme。
假如我们要做一个AppTheme作为程序在theme会写在androidmanifest.xml里面的Application上面,那么我还需要一个base的theme,叫做AppBaseTheme,这个的作用就是为不同版本的android系统做适应,在版本14以前,应该继承Theme.light, 在14以后,应该继承Theme.holo.light,在21以后,就应该继承Theme.material.light。(不一定是light,但就是那个意思)。
比如values目录下的themes.xml下面
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
在values-v14目录下themes.xml, 需要对AppBaseTheme进行重写:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 14 theme customizations can go here. -->
</style>
在values-v21目录下,我们可能需要继承material的theme来适配最新最炫的material design
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<!-- API 21 theme customizations can go here. -->
</style>
正确写法:
<style name="AppTheme.yourTheme">
</style>
错误写法1:
<style name="yourTheme" parent="AppTheme">
</style>
错误写法2:
<style name="Theme.yourTheme" parent="AppTheme">
</style>
4.style规则和theme大致一样,但是应该指定parent为系统的wight的风格。
总结:第1,3条为主要关键,主要的rule没有了,就会越来越乱,加上团队上的每个人对于theme比较轻视,但是却对此认识不足,甚至有人认为做好了效果是对的,那个有什么不对的。但勿以恶小而为之,小细节也应整理清楚,才能导致代码悄悄的腐坏。