[android]theme命名规

本文介绍了在Android项目中如何规范化管理和命名theme,以保持项目的整洁和易于维护。建议将style和theme分别存储在styles.xml和themes.xml文件中,并创建一个总theme(如AppTheme)和基础theme(如AppBaseTheme),根据不同的Android版本进行适配。遵循这些步骤,可以避免主题文件的混乱,提高代码质量。
摘要由CSDN通过智能技术生成

对于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>


3. 有了AppTheme以后,针对一些个别的画面,也许需要一些特别的theme,但是一般原则是应该从AppTheme派生,从AppTheme里面派生的一般应该命名为AppTheme.yourtheme,而不是直接命名为yourtheme或者Theme.yourtheme但是指定parent是AppTheme。

 正确写法:

    <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比较轻视,但是却对此认识不足,甚至有人认为做好了效果是对的,那个有什么不对的。但勿以恶小而为之,小细节也应整理清楚,才能导致代码悄悄的腐坏。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值