简体   繁体   English

如何从Android资源文件向Paint对象添加颜色?

[英]How to add a color from android resource file to Paint object?

I want to know how to add a color from andoid resource file colors.xml to Paint object? 我想知道如何从Andoid资源文件colors.xml向Paint对象添加颜色? The way I do it so far is 我到目前为止的方法是

Paint orange = new Paint();
orange.setColor(getResources().getColor(R.color.orange));

But my problem is that getColor() is deprecated and I do not know which method should be used instead. 但是我的问题是getColor()已被弃用,我不知道应该改用哪种方法。 Did anyone have the same problem and found a solution? 有没有人遇到同样的问题并找到了解决方案?

Thanks in advance! 提前致谢!

Use below code; 使用下面的代码;

     Paint orange = new Paint();
     orange.setColor(ContextCompat.getColor(MainActivity.this, R.color.your_color));

if you use in Activity at that time use below code:- 如果您当时在Activity中使用,请使用以下代码:-

 Paint orange = new Paint();     
 orange.setColor(ContextCompat.getColor(this, R.color.yourcolor));

if you use in Fragment at that time use below code:- 如果您当时在Fragment中使用,请使用以下代码:-

 Paint orange = new Paint();     
 orange.setColor(ContextCompat.getColor(getActivity(), R.color.yourcolor));

您可以使用ContextCompat.getColor(context, R.color.orange)代替(getResources().getColor(R.color.orange)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM