所选部件的主题相关的色彩

所选部件的主题相关的色彩

本文介绍了所选部件的主题相关的色彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我是pretty的肯定,这个问题已经被回答的地方。它只是似乎太普通了。但我无法找到答案。我也不能找出解决方案。

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 的属性(这个文件去的价值文件夹下):

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#0000FF</color>
    <color name="green">#00FF00</color>
</resources>

这就是它!

这篇关于所选部件的主题相关的色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:44