所选小部件的主题相关颜色 [英] Theme dependent colors of selected widgets
问题描述
我很确定这个问题已经在某处得到了回答.这似乎太常见了.但我找不到答案.我也想不出解决办法.
I'm pretty sure that this question already has been answered somewhere. It just seems too common. But I can't find the answer. I can't also figure out the solution.
问题来了:
我希望我的 TableRow 之一具有不同的背景颜色.很简单,我只需要添加
I want one of my TableRow's to have different background color. It's simple, I just need to add
android:background="#123456"
在 TableRow 的 XML 声明中.但是,我也希望我的应用程序有两个主题.在另一个主题中,TableRow 应该有不同的背景颜色.我只是找不到在主题中定义颜色值并使用它的方法.我想输入如下内容:
In TableRow's XML Declaration. But, I also want my application to have two themes. In the other theme, the TableRow should have different background color. I just can't find a way to define a color value inside a theme and use it. I would like to type something like this:
<style name="Theme.MyApp" parent="@style/Theme.Light">
<color "my_cool_color">#123456</color>
</style>
<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
<color "my_cool_color">#654321</color>
</style>
并且,在 TableRow 的声明中:
And, in TableRow's declaration:
android:background="@color/my_cool_color"
所以,当我改变主题时,那个 TableRow 的背景颜色也会改变.我已经在很多方面尝试了很多小时但没有成功......我没有尝试过的一件事是基于 TableRow 创建我自己的小部件并为其声明一个单独的样式 - 我认为这应该有效,但它是对于这么简单的问题来说,解决方案太繁重了.
So, when I change the theme, the color of that one TableRow's background also changes. I've tried in many ways for many hours and didn't succeed... One thing I didn't try, was creating my own widget basing on TableRow and declaring a separate style for it - I think this should work, but it's just too heavy solution for so simple problem.
推荐答案
您可以使用属性来做到这一点.首先在 attrs.xml
中定义您的属性(此文件位于values"文件夹下):
You can do this using attributes. First define your attribute in attrs.xml
(this file goes under the 'values' folder):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myCoolColor" format="color" />
</resources>
然后在您的 styles.xml
中,为每个主题定义 myCoolColor
:
Then in your styles.xml
, define myCoolColor
for each theme:
<style name="Theme.MyApp" parent="@style/Theme.Light">
<item name="myCoolColor">#123456</item>
</style>
<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
<item name="myCoolColor">#654321</item>
</style>
现在,指定 myCoolColor
作为视图的背景:
Now, specify myCoolColor
as the background of your view:
android:background="?myCoolColor"
您可以更进一步,使用对颜色的引用,这样您就可以将颜色定义在一个地方.更改属性以包含引用(请注意,我们可以使用颜色或引用):
You can go further and use a reference to a color so you can keep your colors defined in one place. Change the attribute to include a reference (note that we can use a color OR a reference):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myCoolColor" format="color|reference" />
</resources>
更改您的 styles.xml
以引用每个主题的颜色:
Change your styles.xml
to reference a color for each theme:
<style name="Theme.MyApp" parent="@style/Theme.Light">
<item name="myCoolColor">@color/blue</item>
</style>
<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
<item name="myCoolColor">@color/green</item>
</style>
最后在 colors.xml
中定义颜色:
Finally define the colors in your colors.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#0000FF</color>
<color name="green">#00FF00</color>
</resources>
就是这样!
这篇关于所选小部件的主题相关颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!