具有不同基本主题的Android风格 [英] Android flavors with different base themes

查看:124
本文介绍了具有不同基本主题的Android风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在处理的多种口味的android项目.

I've an android project with multiple flavors that I'm working on.

这很好用,我可以自定义应用程序的元素,例如颜色和字符串资源.

This works fine and I can customize elements of the app such as colors and string resources.

我要使其成为某种口味,以使某些口味基于AppCompat浅色主题,而某些口味基于AppCompat深色主题.

I'm wanting to make it so that some of the flavors are based on the AppCompat light theme and some on the AppCompat dark theme.

我知道我可以通过重复我的样式和每种口味中的所有<items>来做到这一点,将主题设置为指向每个应用的自定义清单中的每个主题,但这似乎有些过分.

I know I could do this by repeating all the <items> in my style and in each flavor setting the theme to point to each one in a custom manifest for each app but that seems overkill.

是否有一些简单的方法可以设置主题

Is there some easy way to set up a theme as such

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">                
            <item name="colorPrimary">@color/primary_colour_tone1</item>
            ...lots more settings
    </style>

但是不是直接指向Theme.AppCompat.NoActionBar指向可以在每个gradle构建中进行不同设置的引用吗?

But instead of directly pointing to Theme.AppCompat.NoActionBar point to a reference that in each gradle build can be set differently?

类似这样的东西:

    <style name="AppTheme" parent="**Theme reference here**">                
            <item name="colorPrimary">@color/primary_colour_tone1</item>
            ...lots more settings
    </style>

我曾尝试使用@ string/theme_name之类的方法,但这不起作用,有没有简单的方法可以做到这一点?

I've tried using things like @string/theme_name but that doesn't work, is there a simple way to do this?

推荐答案

您可以为一种或多种口味的通用资源(也包括资源)定义sourceSets,这意味着您不必为所有口味重复所有资源.

You can define sourceSets for common resources (also sources) for one or more flavors which means you don't have to repeat all resources for all flavors.

例如,您有3种口味. flavor1flavor2使用相同的主题,但flavor3.然后,您可以为常见资源定义额外的源集,例如commonA(对于flavor1,flavor2)和commonB(对于flavor3):

For instance, you have 3 flavors. flavor1 and flavor2 uses same theme but flavor3. Then you can define extra source set for common resources, such as commonA (for flavor1, flavor2) and commonB(for flavor3):

...
android {
    ...
    productFlavors {
        flavor1{

        }
        flavor2{

        }
        flavor3{

        }
    }

    sourceSets.flavor1{
        res.srcDirs = ['res', 'src/commonA/res']
    }
    sourceSets.flavor2{
        res.srcDirs = ['res', 'src/commonA/res']
    }
    sourceSets.flavor3{
        res.srcDirs = ['res', 'src/commonB/res']
    }
}

还应该创建文件夹src/commanAsrc/commonB,然后将公用资源放入它们的res文件夹中.

Also you should create the folders src/commanA and src/commonB then put your common resources into their res folders.

这篇关于具有不同基本主题的Android风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆