从颜色资源中获取color-int

ata*_*ulm 413 android colors

有没有办法从颜色资源中获取color-int?我试图获取资源(R.color.myColor)中定义的颜色的单个红色,蓝色和绿色组件,以便我可以将三个搜索栏的值设置为特定级别.


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

使用@ sat的正确答案:

__PRE__

sat*_*sat 871

您可以使用:

getResources().getColor(R.color.idname);
Run Code Online (Sandbox Code Playgroud)

在这里查看如何定义自定义颜色:

http://sree.cc/google/android/defining-custom-colors-using-xml-in-android

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

ContextCompat.getColor(context, R.color.your_color);
Run Code Online (Sandbox Code Playgroud)

(在支持库23中添加)

EDIT(2):

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

ResourcesCompat.getColor(getResources(), R.color.your_color, null); //without theme

ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with theme
Run Code Online (Sandbox Code Playgroud)

  • getColor()现已弃用,您可以使用:ContextCompat.getColor(context,R.color.your_color); (29认同)
  • @Blundell呃,如果你现在需要它,不知道但这适用于`android.R.color.some_color`例如:`getResources().getColor(android.R.color.holo_blue_bright)`(至少在API 17上) (17认同)
  • 为什么Google觉得有必要为这个糟糕的应用程序紧凑型库弃用一个非常好的函数.它很糟糕,两者兼而有之. (11认同)
  • 我永远对这个平台的残暴感到敬畏……无言以对。 (9认同)
  • 那么android.R.color.some_color :-( (6认同)
  • 我知道您不是进行编辑的人,但是“ ContextCompat”和“ ResourcesCompat”之间有什么区别?如果没有实际的区别,那么从答案中删除其中之一就可以减少混乱。 (2认同)

Ult*_*o_m 111

基于新的Android支持库(以及更新),现在您应该致电:

ContextCompat.getColor(context, R.color.name.color);
Run Code Online (Sandbox Code Playgroud)

根据文件:

public int getColor (int id)
Run Code Online (Sandbox Code Playgroud)

此方法在API级别23中已弃用.请改用getColor(int,Theme)

它是同样的解决方案getResources().getColorStateList(id):

你必须改变它:

ContextCompat.getColorStateList(getContext(),id);
Run Code Online (Sandbox Code Playgroud)

  • 对于那些想知道在新方法中作为主题填写什么的人来说,`Theme`可以作为null传递,所以如果你不确定要传递什么主题,只需调用`getColor(R.color.my_color,null)`. (10认同)

Sur*_*gch 33

定义你的颜色

值/ color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- color int as #AARRGGBB (alpha, red, green, blue) -->
    <color name="orange">#fff3632b</color>
    ...
    <color name="my_view_color">@color/orange</color>

</resources>
Run Code Online (Sandbox Code Playgroud)

获取颜色int并设置它

int backgroundColor = ContextCompat.getColor(context, R.color.my_view_color);
// Color backgroundColor = ... (Don't do this. The color is just an int.)

myView.setBackgroundColor(backgroundColor);
Run Code Online (Sandbox Code Playgroud)

也可以看看

  • @Zapnologica,查看[this question](http://stackoverflow.com/questions/7666589/using-getresources-in-non-activity-class)的答案,了解在Activity之外使用`getResources()`的想法或分段. (2认同)

Bla*_*nka 9

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

//example
textView.setTextColor(getColor(R.color.green));
// if context is not available(ex: not in activity) use with context.getColor()
Run Code Online (Sandbox Code Playgroud)

如果你想要低于 API 级别23,只需使用这个:

textView.setTextColor(getResources().getColor(R.color.green));
Run Code Online (Sandbox Code Playgroud)

但请注意,它getResources().getColor()在 API Level 中已被弃用23。在这种情况下,将上面替换为:

textView.setTextColor(ContextCompat.getColor(this /*context*/, R.color.green)) //Im in an activity, so I can use `this`
Run Code Online (Sandbox Code Playgroud)

ContextCompat:用于访问功能的帮助程序Context

如果您愿意,您可以使用SDK_INT如下约束:

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


Khe*_*raj 6

最佳方法

作为@sat的答案,获得颜色的好方法是

ResourcesCompat.getColor(getResources(), R.color.your_color, null);
Run Code Online (Sandbox Code Playgroud)

or use below way when you don't have access to getResources() method.

Context context  = getContext(); // like Dialog class
ResourcesCompat.getColor(context.getResources(), R.color.your_color, null);
Run Code Online (Sandbox Code Playgroud)

What i do is

public void someMethod(){
    ...
    ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
}
Run Code Online (Sandbox Code Playgroud)

It is most simple to use anywhere in your app! Even in Util class or any class where you don't have Context or getResource()

Problem (When you don't have Context)

When you don't have Context access, like a method in your Util class.

Assume below method without Context.

public void someMethod(){
    ...
    // can't use getResource() without Context.
}
Run Code Online (Sandbox Code Playgroud)

Now you will pass Context as a parameter in this method and use getResources().

public void someMethod(Context context){
    ...
    context.getResources...
}
Run Code Online (Sandbox Code Playgroud)

So here is a Bonus unique solution by which you can access resources from anywhere like Util class . Add Resources to your Application class or Create one if does not exist.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Add name field to your manifest.xml <application tag. (If not added already)

<application
        android:name=".App"
        ...
        >
        ...
    </application>
Run Code Online (Sandbox Code Playgroud)

Now you are good to go. Use ResourcesCompat.getColor(App.getRes(), R.color.your_color, null); anywhere in app.


nin*_*pie 5

我已更新为使用,ContextCompat.getColor(context, R.color.your_color);但有时(在某些设备/ Android版本上。我不确定)会导致NullPointerExcepiton。

因此,要使其在所有设备/版本上都能正常工作,在使用空指针的情况下,我会采用旧的方法。

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));
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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

Color.parseColor(getString(R.color.idname);
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,没有意识到你可以通过这种方式获得字符串形式的颜色。我不认为这更容易,但很有趣 (2认同)