美文网首页Android知识
Android Themes & styles 架构

Android Themes & styles 架构

作者: Henryhaoson | 来源:发表于2017-06-04 12:25 被阅读0次

Android Themes & styles, a real architecture(译)

在当前的android开发中,有很多框架十分流行,比如说一些架构MVP,MVVM或者clean架构,又比如说Rxjava,Dagger,还有google二儿子Kotlin。
但是我们是否是注意到,当我们构建项目时,我们对Theme和Style的构建时还是用的老一套的方法,我们并没有考虑过如何构建他们,因为我们往往更注重我们的java代码,那些xml代码经常被我们忽略。

一个经常犯的错误

当新建一个项目时,androidStudio会帮我们构建一个初始的AppTheme的style.xml文件。当你写项目不断的进行开发迭代,你的style.xml文件里会被你塞满许多属性配置。
但是有一点,当你的项目的最低sdk为16,而你又想使用api19里面的特性时,比如说windowTransluscentStatus,你不能在文件里直接配置,这样会报错。

image

在android开发的时候,你可以创建特定的API文件夹,用来存放特定API的配置。比如说你可以创建一个res/values-v19/文件夹用来存放一个新的sytle.xml文件,当运行在API为19的手机上,res/values-v19/这个文件夹比res/values/有更高的优先级,也就是说首先会加载res/values-v19/

image

所有说现在当我们遇到上面的问题的时候,我们可以按照下面的步骤来。

  • 复制原来sytle.xml里面的所有属性的配置。
  • 粘贴到新建的-v19文件夹里的style.sml里。
  • 在新建的style.xml文件里添加新的属性配置。

按照上面的方法,你可以实现对应的API加载对应的sytle,你确实可以添加你的新特性。但是!!!请你不要这么做,因为当你的配置越来越多的时候,你会疯掉的,你必须每次添加新的配置的时候,还要记得复制到你的-v19文件夹里。
还有一种解决方法

  • res/values/styles.xml里建一个BaseAppTheme,里面添加除了特定API性质的所有属性
  • 在相同文件下改变AppTheme 让他继承BaseAppTheme
  • 在你的-v19里,你只要也让你的AppTheme继承BaseAppTheme,
  • 在-v19里面添加你需要使用的新特性。

如果你使用上面的方法,你就可以自由自在的添加新特新而不需要担心拷贝的问题了。这多好。

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/windowBackground</item>
    </style>
    <style name="AppTheme" parent="BaseAppTheme"/>
</resources>

values-v19/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="BaseAppTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

更高API出现的问题

假如说你现在使用的是更高级的API21,你想要使用里面的新特性,比如说共享元素(这个东西确实很炫酷)。
你现在学了上面的只是肯定会新建一个-v21文件夹。在里面加入你的新特性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="BaseAppTheme">
        <item name="android:windowSharedElementEnterTransition">@android:animator/fade_in</item>
    </style>
</resources>

现在当app运行在android5.0系统的手机上时,你会发现你的状态栏变了,因为你的手机使用的AppTheme是-v21的属性,他会首先加载-v21的属性配置,但是-v21并没有继承-v19,所以并不会加载-v19d 属性配置。

theme继承链

为了解决上面的问题,我们来重写themes,让不同的API采用链式继承方式来实现所有的属性配置

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Base.V0.AppTheme"/>

    <style name="Base.V0.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Generic, non-specific attributes -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/windowBackground</item>
    </style>
</resources>

values-v19/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Base.V19.AppTheme"/>
    
    <style name="Base.V19.AppTheme" parent="Base.V0.AppTheme">
        <!-- API 19 specific attributes -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

values-v21/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Base.V21.AppTheme"/>
    
    <style name="Base.V21.AppTheme" parent="Base.V19.AppTheme">
        <!-- API 21 specific attributes -->
        <item name="android:windowSharedElementEnterTransition">@android:animator/fade_in</item>
    </style>
</resources>

使用这种主题架构,对于每个API级别,AppTheme将具有来自所有API级别的所有属性,并且以后很容易扩展和添加新的api级别特定属性。

相关文章

网友评论

    本文标题:Android Themes & styles 架构

    本文链接:https://www.haomeiwen.com/subject/cppvfxtx.html