我如何编程方式更改colors.xml颜色的价值? [英] How can I programmatically change the value of a color in colors.xml?

查看:172
本文介绍了我如何编程方式更改colors.xml颜色的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,将用不同的颜色和图像为不同的客户可以为主题的应用程序。在编译的时候,而我不得不选择重新编写的自定义颜色的colors.xml文件,我对在运行时设置的颜色倾斜。我想知道是,如果这是一些方法来编程更改colors.xml文件中定义的颜色的值,并有新的值取在当在布局定义中使用的所有地方的效果。

I am developing an application that will be themed with different colors and images for different clients. While I have the option to re-write the colors.xml file with the custom colors at build time, I am leaning towards setting up the colors at runtime. What I am wondering is if that is some way to programmatically change the value of a color defined in the colors.xml file and have that new value take effect in ALL places where it is used in the layout definition.

因此​​,换句话说,如果我有:

So in other words if I have:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color headerColor="white">#FFF</color>
    <color backgroundColor="black">#000</color>
</resources>

和喜欢的东西布局文件:

And a layout file with something like:

<TextView
        android:id="@+id/listItemHeaderActivity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textColor="@color/headerColor"
        android:background="@color/backgroundColor"
        android:text="@string/listTextHeaderActivity"/>

我可以改变的值 headercolor 的backgroundColor Java和有它发生在所有使用次数这些价值观?或将我必须分别更改各相关查看颜色每当我展示这些观点?

Can I change the value of headercolor and backgroundColor in Java and have it take place in all Views that use those values? Or will I have to change each relevant View color individually whenever I display those views?

先谢谢了。

推荐答案

如果你描述了为什么以及如何使用这个动态色彩,为什么你想在运行时,而不是编译时要做到这一点这将有助于。不过,假设它必须是你问的方式...

It would help if you described why and how you are using this dynamic color, and why you want to do this at runtime instead of build time. But assuming it has to be the way you asked...

我建议写一个辅助函数,所有的地方都可以调用,设置它们。

I suggest writing a helper function that all places can call and set them.

public int getMyColor(...) {
    // figure out which color to use, via a database call,
    // an asset load, some algorithm, or whatever you need
    ...

    // once color chosen, create an RGBA integer for it
    final int myColor = ...

    return myColor;
}

现在这个叫每个活动/分段需要它,并根据需要在适当的意见设置的颜色属性(S)。 (使用 View.setBackgroundColor(...)等)

Now call this on every activity/fragment that needs it and set the color attribute(s) on the appropriate views as needed. (With View.setBackgroundColor(...), etc.)

然而,让这个以XML设置和/或发展布局previews工作,你就必须编写自定义视图类来调用辅助函数了。不同的地方,你将如何使用这种颜色,它可能不值得的。

However, to allow this to work in XML settings and/or development layout previews, you would have to write a custom view class to call that helper function too. Depending on where and how you will be using this color, it may not be worth it.

这解决方案是不是很优雅,需要大量调用每一个活动/分段需要它这个定制getMyColor的辅助功能。如果只是设置一个或两个地方虽然,这可能不是一个大问题。同样,知道的为什么的你想做的事,而不是要求我们如何去做这可能会产生一个更好的选择适合你。

This solution isn't very elegant and requires a lot of calling this custom getMyColor helper function in every activity/fragment that needs it. If it is only set one or two places though, it's probably not a big deal. Again, knowing why you want to do this instead of asking us how to do it, may yield a better alternative for you.

例如,这不是一个回答你的问题,但你有没有想过主题?它仍然有其在构建时设置它们,除非你希望所有上述情况,但是这取决于你如何使用这个颜色,这可能是比我上面提到的一塌糊涂更好的问题。

For example—this isn't an answer to your question—but have you thought about themes? It will still have the problem of having to set them at build time unless you want all of the above, but depending on how you're using this color, it might be better than the mess I outlined above.

这篇关于我如何编程方式更改colors.xml颜色的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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