如何从颜色资源中获取颜色整数? [英] How can I get color-int from color resource?
问题描述
有没有办法从颜色资源中获取颜色整数?
Is there any way to get a color-int from a color resource?
我正在尝试获取资源 (R.color.myColor) 中定义的颜色的各个红色、蓝色和绿色分量,以便我可以将三个搜索栏的值设置为特定级别.
I am trying to get the individual red, blue and green components of a color defined in the resource (R.color.myColor) so that I can set the values of three seekbars to a specific level.
推荐答案
您可以使用:
getResources().getColor(R.color.idname);
在此处查看如何定义自定义颜色:
Check here on how to define custom colors:
http://sree.cc/google/android/defining-custom-colors-using-xml-in-android
编辑(1):由于 getColor(int id)
现在不推荐使用,所以必须使用它:
EDIT(1):
Since getColor(int id)
is deprecated now, this must be used :
ContextCompat.getColor(context, R.color.your_color);
(在支持库 23 中添加)
(added in support library 23)
编辑(2):
以下代码可用于棉花糖前后(API 23)
Below code can be used for both pre and post Marshmallow (API 23)
ResourcesCompat.getColor(getResources(), R.color.your_color, null); //without theme
ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with theme
这篇关于如何从颜色资源中获取颜色整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!