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

发布于 2024-10-21 03:43:30 字数 108 浏览 10 评论 0 原文

有什么方法可以从颜色资源中获取 color-int 吗?

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

Is there any way to get a color-int from a color resource?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

绿萝 2024-10-28 03:43:31

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

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

如果您想要低于 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`

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

If your current min. API level is 23, you can simply use getColor() like we are using for getString():

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

If you want below API level 23, just use this:

textView.setTextColor(getResources().getColor(R.color.green));

But note that getResources().getColor() is deprecated in API Level 23. In that case replace above with:

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

ContextCompat: Helper for accessing features in Context

If You want, you can constraint with SDK_INT like below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    textView.setTextColor(getColor(R.color.green));
} else {
    textView.setTextColor(getResources().getColor(R.color.green));
}
不弃不离 2024-10-28 03:43:31

我更新为使用 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));
    }
}

I updated to use ContextCompat.getColor(context, R.color.your_color); but sometimes (On some devices/Android versions. I'm not sure) that causes a NullPointerExcepiton.

So to make it work on all devices/versions, I fall back on the old way of doing it, in the case of a null pointer.

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));
    }
}
凑诗 2024-10-28 03:43:31

有关可能有助于在搜索结果中显示此问题的另一个用例的更多信息,我想将 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)
);

For more information on another use-case that may help surface this question in search results, I wanted to apply alpha to a color defined in my resources.

Using @sat's correct answer:

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)
);
你怎么这么可爱啊 2024-10-28 03:43:31
ContextCompat.getColor(context, R.color.your_color);

活动中

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

在片段的

ContextCompat.getColor(getActivity(), R.color.your_color);

,例如:

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

in activity

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

in fragment

ContextCompat.getColor(getActivity(), R.color.your_color);

for example:

tvsun.settextcolour(ContextCompat.getColor(getActivity(), R.color.your_color))
趴在窗边数星星i 2024-10-28 03:43:31

从非活动类访问颜色可能很困难。我发现的替代方案之一是使用enumenum 提供了很大的灵活性。

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

Accessing colors from a non-activity class can be difficult. One of the alternatives that I found was using enum. enum offers a lot of flexibility.

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;
  }
}
迷迭香的记忆 2024-10-28 03:43:31

最近的工作方法:

getColor(R.color.snackBarAction)

Most Recent working method:

getColor(R.color.snackBarAction)
寻找一个思念的角度 2024-10-28 03:43:31

或者如果您有一个函数(字符串文本,字符串颜色)并且您需要传递资源颜色字符串,您可以执行以下操作

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

or if you have a function(string text,string color) and you need to pass the Resource Color String you can do as follow

String.valueOf(getResources().getColor(R.color.enurse_link_color))
吾家有女初长成 2024-10-28 03:43:31

在 kotlin 中,只需在您的活动中使用它

R.color.color_name

即可

mytextView.setTextColor(R.color.red_900)

In kotlin just use this in your activity

R.color.color_name

ex-

mytextView.setTextColor(R.color.red_900)
带上头具痛哭 2024-10-28 03:43:30

您可以使用:

getResources().getColor(R.color.idname);

查看此处了解如何定义自定义颜色:

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

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

ContextCompat.getColor(context, R.color.your_color);

在支持库 23 中添加)

编辑(2):

下面的代码可用于棉花糖前和后 (API 23)

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

ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with theme

You can use:

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

EDIT(1):
Since getColor(int id) is deprecated now, this must be used :

ContextCompat.getColor(context, R.color.your_color);

(added in support library 23)

EDIT(2):

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
懒的傷心 2024-10-28 03:43:30

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

ContextCompat.getColor(context, R.color.name.color);

根据文档

public int getColor (int id)

此方法在 API 级别 23 中已弃用。
使用 getColor(int, Theme) 代替

它与 getResources().getColorStateList(id) 的解决方案相同:

您必须像这样更改它:

ContextCompat.getColorStateList(getContext(),id);

EDIT 2019

关于 ThemeOverlay 使用最近视图的上下文:

val color = ContextCompat.getColor(
  closestView.context,
  R.color.name.color
)

这样您就可以根据您的 ThemeOverlay 获得正确的颜色。

当您在同一活动中使用不同的主题(例如深色/浅色主题)时特别需要。如果您想了解有关主题和样式的更多信息,建议观看此演讲:使用风格开发主题

Nick Butcher - Droidcon Berlin - 开发风格主题

Based on the new Android Support Library (and this update), now you should call:

ContextCompat.getColor(context, R.color.name.color);

According to the documentation:

public int getColor (int id)

This method was deprecated in API level 23.
Use getColor(int, Theme) instead

It is the same solution for getResources().getColorStateList(id):

You have to change it like this:

ContextCompat.getColorStateList(getContext(),id);

EDIT 2019

Regarding ThemeOverlay use the context of the closest view:

val color = ContextCompat.getColor(
  closestView.context,
  R.color.name.color
)

So this way you get the right color based on your ThemeOverlay.

Specially needed when in same activity you use different themes, like dark/light theme. If you would like to understand more about Themes and Styles this talk is suggested: Developing Themes with Style

Nick Butcher - Droidcon Berlin - Developing Themes with Style

画▽骨i 2024-10-28 03:43:30

定义颜色

values/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>

获取颜色 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);

另请参阅

Define your color

values/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>

Get the color int and set it

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

See also

吝吻 2024-10-28 03:43:30

找到了一种更简单、也有效的方法:

Color.parseColor(getString(R.color.idname));

Found an easier way that works as well:

Color.parseColor(getString(R.color.idname));
來不及說愛妳 2024-10-28 03:43:30

最佳方法

正如 @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);

我所做的是

public void someMethod(){
    ...
    ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
}

在您的应用程序中的任何地方使用都是最简单的!即使在 Util 类或任何没有 Context 或 getResource() 的类中也会

出现问题(当您没有 Context 时)

您没有 Context 访问权限时,就像 Util 类中的方法一样。

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

public void someMethod(){
    ...
    // can't use getResource() without Context.
}

现在,您将在此方法中将 Context 作为参数传递,并使用 getResources()。

public void someMethod(Context context){
    ...
    context.getResources...
}

因此,这是一个额外的独特解决方案,您可以通过它访问来自任何地方的资源,例如 Util class
Resources 添加到您的 Application 类中,如果不存在,则创建一个。

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 标记中。 (如果尚未添加)

<application
        android:name=".App"
        ...
        >
        ...
    </application>

现在您就可以开始了。在应用程序中的任何位置使用 ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);

Best Approach

As @sat answer, good approach for getting color is

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

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

What i do is

public void someMethod(){
    ...
    ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
}

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

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

public void someMethod(Context context){
    ...
    context.getResources...
}

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

}

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

<application
        android:name=".App"
        ...
        >
        ...
    </application>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文