如何将主题资源应用于多个主题?

时间:2015-06-06 18:38:04

标签: android android-theme android-styles

如果在应用中使用单个父主题,则每个主题都有自己的资源的用户可选主题(明亮和黑暗)为straightforward to implement

<style name="MyApp.Dark" parent="android:Theme.Material">
    <item name="themeBackground">@color/dark_grey</item>
    <item name="themeBackgroundTransparent">@color/darkColorBackgroundTransparent</item>
    <item name="colorPrimary">@color/darkColorPrimary</item>
    .
    .
</style>

<style name="MyApp.Light" parent="android:Theme.Material.Light">
    <item name="themeBackground">@color/brown_50</item>
    <item name="themeBackgroundTransparent">@color/lightColorBackgroundTransparent</item>
    <item name="colorPrimary">@color/lightColorPrimary</item>
    .
    .
</style>

但是,典型的Android应用可能会在整个应用中使用多个父主题,例如对话框主题,无操作栏主题或大对话时对话框。扩展Theme.Material的当前主题不会涵盖这些主题(因为继承是昂贵的),因此使用这些主题必须明暗主题的其他样式定义:

<style name="MyApp.DialogWhenLarge.Light" parent="android:Theme.Material.Light.DialogWhenLarge">
</style>

<style name="MyApp.DialogWhenLarge.Dark" parent="android:Theme.Material.DialogWhenLarge">
</style>

为了将主题资源应用于主题,简单的解决方案是将MyApp.Dark主题中的各个项目复制并粘贴到新的MyApp.DialogWhenLarge.Dark主题中,等等。但是,随着主题和资源数量的增加,重复代码的数量变得难以管理,难以理解且容易出错。

所以我的问题是,将主题资源应用于多个主题定义的简单方法是什么?

理想情况下,人们可以将一组资源应用于主题,就像将一个样式应用于视图一样,这将产生以下形式的解决方案:

<style name="MyApp.DialogWhenLarge.Light" parent="android:Theme.Material.Light.DialogWhenLarge">
    <item name="themedResources">[pointer to canonical list of light theme resources]
</style>

当然,任何解决方案都保留以?themeBackground

格式从视图中引用主题资源的能力也很重要。

0 个答案:

没有答案