如何从颜色资源中获取color-int? [英] 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)
EDIT(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
这篇关于如何从颜色资源中获取color-int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!