Android 如何从颜色资源中获取颜色int?

Android 如何从颜色资源中获取颜色int?,android,colors,android-resources,Android,Colors,Android Resources,有没有办法从颜色资源中获取颜色int 我试图获取资源(R.color.myColor)中定义的颜色的单个红色、蓝色和绿色分量,以便将三个seekbar的值设置为特定级别。您可以使用: getResources().getColor(R.color.idname); 在此处查看如何定义自定义颜色: 编辑(1): 由于getColor(int-id)现在已被弃用,因此必须使用: ContextCompat.getColor(context, R.color.your_color); (在支持库

有没有办法从颜色资源中获取颜色int

我试图获取资源(R.color.myColor)中定义的颜色的单个红色、蓝色和绿色分量,以便将三个seekbar的值设置为特定级别。

您可以使用:

getResources().getColor(R.color.idname);
在此处查看如何定义自定义颜色:

编辑(1): 由于
getColor(int-id)
现在已被弃用,因此必须使用:

ContextCompat.getColor(context, R.color.your_color);
(在支持库23中添加)

编辑(2):

以下代码可用于棉花糖前处理和后处理(API 23)

定义你的颜色 values/color.xml

另见

基于新的Android支持库(和更新),现在您应该调用:

ContextCompat.getColor(context, R.color.name.color);
根据报告:

此方法在API级别23中被弃用。 改为使用getColor(int,Theme)

对于
getResources(),这是相同的解决方案。getColorStateList(id)

您必须这样更改它:

ContextCompat.getColorStateList(getContext(),id);
编辑2019

关于<代码>最小视图使用最近视图的上下文:

val color = ContextCompat.getColor(
  closestView.context,
  R.color.name.color
)
因此,通过这种方式,您可以根据您的ThemeOverlay获得正确的颜色

在同一活动中,特别需要使用不同的主题,如黑暗/光明主题。如果您想了解更多关于主题和风格的信息,建议您参加以下讲座:


我更新为使用
ContextCompat.getColor(context,R.color.your\u color)但有时(在某些设备/Android版本上,我不确定)会导致NullPointerExceptan

因此,为了使它在所有设备/版本上都能工作,我采用了旧的方法,在空指针的情况下

try {
    textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_grey_dark));
}
catch(NullPointerException e) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        textView.setTextColor(getContext().getColor(R.color.text_grey_dark));
    }
    else {
        textView.setTextColor(getResources().getColor(R.color.text_grey_dark));
    }
}
最佳方法 正如@sat回答的那样,获得颜色的好方法是

ResourcesCompat.getColor(getResources(), R.color.your_color, null);
或者在您无权访问
getResources()
方法时使用下面的方法

Context context  = getContext(); // like Dialog class
ResourcesCompat.getColor(context.getResources(), R.color.your_color, null);
我所做的是 在应用程序中的任何位置使用它都是最简单的!即使在Util类或任何没有上下文或getResource()的类中

问题(当您没有上下文时) 当您没有
上下文
访问权限时,就像
Util
类中的方法一样

假设下面的方法没有上下文

public void someMethod(){
    ...
    // can't use getResource() without Context.
}
现在,您将在该方法中将
Context
作为参数传递,并使用
getResources()

因此,这里有一个独特的奖金解决方案,通过它,您可以从任何地方访问资源,如
Util类
。 将
资源
添加到
应用程序
类中,如果不存在,则创建一个

import android.app.Application;
import android.content.res.Resources;

public class App extends Application {
    private static App mInstance;
    private static Resources res;


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        res = getResources();
    }

    public static App getInstance() {
        return mInstance;
    }

    public static Resources getResourses() {
        return res;
    }

}

将名称字段添加到您的
manifest.xml
从非活动类访问颜色可能比较困难。我发现的一个替代方法是使用
enum
<代码>枚举
提供了很大的灵活性

public enum Colors
{
  COLOR0(0x26, 0x32, 0x38),    // R, G, B
  COLOR1(0xD8, 0x1B, 0x60),
  COLOR2(0xFF, 0xFF, 0x72),
  COLOR3(0x64, 0xDD, 0x17);


  private final int R;
  private final int G;
  private final int B;

  Colors(final int R, final int G, final int B)
  {
    this.R = R;
    this.G = G;
    this.B = B;
  }

  public int getColor()
  {
    return (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
  }

  public int getR()
  {
    return R;
  }

  public int getG()
  {
    return G;
  }

  public int getB()
  {
    return B;
  }
}
公共枚举颜色
{
颜色0(0x26、0x32、0x38),//R、G、B
颜色1(0xD8、0x1B、0x60),
颜色2(0xFF,0xFF,0x72),
颜色3(0x64、0xDD、0x17);
私人终审法院;
私人终审法院;
私人最终int B;
颜色(最终整数R、最终整数G、最终整数B)
{
这个。R=R;
这个.G=G;
这个.B=B;
}
public int getColor()
{
返回(R&0xff)
活跃

ContextCompat.getColor(actvityname.this, R.color.your_color);
零碎

ContextCompat.getColor(getActivity(), R.color.your_color);
例如:

tvsun.settextcolour(ContextCompat.getColor(getActivity(), R.color.your_color))

有关另一个可能有助于在搜索结果中显示此问题的用例的更多信息,我想将alpha应用于我的参考资料中定义的颜色

使用@sat的正确答案:

int alpha = ... // 0-255, calculated based on some business logic
int actionBarBackground = getResources().getColor(R.color.actionBarBackground);
int actionBarBackgroundWithAlpha = Color.argb(
        alpha,
        Color.red(actionbarBackground),
        Color.green(actionbarBackground),
        Color.blue(actionbarBackground)
);

最近的工作方法:

getColor(R.color.snackBarAction)

如果您当前的最小API级别是23,您可以简单地使用
getColor()
,就像我们在
getString()
中使用的那样:

如果您想要低于API级别
23
,只需使用以下命令:

textView.setTextColor(getResources().getColor(R.color.green));
但请注意,
getResources().getColor()
在API级别
23
中不推荐使用。在这种情况下,请将上述内容替换为:

textView.setTextColor(ContextCompat.getColor(this /*context*/, R.color.green)) //Im in an activity, so I can use `this`
:用于访问中的功能的帮助器

如果需要,可以使用
SDK\u INT
进行约束,如下所示:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    textView.setTextColor(getColor(R.color.green));
} else {
    textView.setTextColor(getResources().getColor(R.color.green));
}

找到了一种更简单的方法:

Color.parseColor(getString(R.Color.idname);
或者,如果您有一个函数(字符串文本、字符串颜色),并且需要传递资源颜色字符串,则可以按如下操作

String.valueOf(getResources().getColor(R.color.enurse_link_color)

android.R.color.some_color:-(@Blundell uhh,不知道你现在是否需要它,但这也适用于
android.R.color.some_color
,例如:
getResources().getColor(android.R.color.holo_blue_bright)
(至少,在API 17上)getColor()现在不推荐使用,你可以使用:ContextCompat.getColor(context,R.color.your_color);为什么谷歌觉得有必要为这个糟糕的应用程序压缩库否决一个完美的函数。它很糟糕,两者都有。我永远敬畏这个平台的残暴……不知所措。你只能使用
getResources()
活动
片段
?@Zapnologica,请参阅的答案,了解在活动或片段之外使用
getResources()
的想法。@Zapnologica编号
getResources()
也可以作为任何实现上下文和视图的公共API提供。对于那些想知道在新方法中填充什么作为主题的人,
theme
可以作为null传递,所以只需调用
getColor(R.color.my_color,null)
如果您不确定要传递什么主题。嗯……这是每个人都说的,但我无法让它工作。我必须初始化上下文吗?当前我得到“无法解析符号‘上下文’”,为了确保您做得正确,请尝试在活动的onCreate中调用它,而不是要获取上下文,您需要调用getContext()或“this”为什么不在所有情况下都使用旧版本,或者如果您仍在检查版本,请使用新的API
Resources.getColor(int,Theme)
(如果可以的话)?您不应该捕获运行时异常。我想只是OCD。在我看来,ContextCompat似乎是一种经得起未来考验的方法,因此是正确的方法。因此,我的方法是,这样做
ContextCompat.getColor(getActivity(), R.color.your_color);
tvsun.settextcolour(ContextCompat.getColor(getActivity(), R.color.your_color))
int alpha = ... // 0-255, calculated based on some business logic
int actionBarBackground = getResources().getColor(R.color.actionBarBackground);
int actionBarBackgroundWithAlpha = Color.argb(
        alpha,
        Color.red(actionbarBackground),
        Color.green(actionbarBackground),
        Color.blue(actionbarBackground)
);
getColor(R.color.snackBarAction)
//example
textView.setTextColor(getColor(R.color.green));
// if context is not available(ex: not in activity) use with context.getColor()
textView.setTextColor(getResources().getColor(R.color.green));
textView.setTextColor(ContextCompat.getColor(this /*context*/, R.color.green)) //Im in an activity, so I can use `this`
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    textView.setTextColor(getColor(R.color.green));
} else {
    textView.setTextColor(getResources().getColor(R.color.green));
}
String.valueOf(getResources().getColor(R.color.enurse_link_color)