Android的使用自定义主题修改样式属性 [英] Android Use custom themes to modify style attributes

查看:518
本文介绍了Android的使用自定义主题修改样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建两个主题,GrayTheme和RedTheme,修改一个样式属性。比如这里有我的两个主题,默认字体颜色为白色,这是罚款,这两个主题:

I want to create two themes, GrayTheme and RedTheme, that modify one style attribute. For example here are my two themes, default font color is white, which is fine for both themes:

<style name="RedTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/white</item>
</style>

<style name="GrayTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/white</item>
</style>

但我有我使用的标题TextViews风格。如果我现在用的是RedTheme我想要的风格HeaderFont有文字颜色红色,并且如果它是GrayTheme我想HeaderFont文字颜色为灰色,没有我不必修改访问此HeaderFont风格单独的XML文件。

But I have a style that I use for Header TextViews. If I am using the RedTheme I want the style HeaderFont to have textColor red and if it is the GrayTheme I want the HeaderFont textColor to be gray, without me having to modify the individual xml files that access this HeaderFont style.

<style name="HeaderFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/gray</item>
</style>

我一直在寻找四处寻找一个优雅的解决方案,这一点,但一直没能找到任何东西。想法?

I've been searching around looking for a graceful solution to this, but haven't been able to find anything. Ideas?

推荐答案

找到一个干净的解决方案,我终于发现,精美的作品答案的多次失败后。

After many failures of finding a clean solution I finally found an answer that works beautifully.

我创建了一个自定义属性

I created a custom attribute

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="headerFontColor" format="reference|color" />
</resources>

然后在我的风格添加了自定义属性

Then in the style I added the custom attribute

<style name="HeaderFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">?headerFontColor</item>
</style>

然后,我只是添加了新的属性主题

Then I simply added the new attribute to the themes

<style name="RedTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/white</item>
    <item name="headerFontColor">@color/red</item>
</style>
<style name="GrayTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/white</item>
    <item name="headerFontColor">@color/gray</item>
</style>

该解决方案指导我:设置颜色基于主题

这篇关于Android的使用自定义主题修改样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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