简体   繁体   English

如何在Android中设置自定义颜色

[英]How to set custom color in android

This is my res/values/color.xml 这是我的res / values / color.xml

 <resources>
    <color name="ColorPrimary">#FF5722</color>
</resources>

this is .java file 这是.java文件

Color color = context.getResources().getColor(R.color.ColorPrimary); //Error
    textView.setTextColor(color);

It's giving me an error. 给我一个错误。 Required android.graphics.Color. 必需的android.graphics.Color。 Found int 发现int

How can I handle it? 我该如何处理?

The getColor method returns an int, but you try to store it in a Color object. getColor方法返回一个int,但是您尝试将其存储在Color对象中。 I suggest you simply do this: 我建议您只是这样做:

textView.setTextColor(context.getResources().getColor(R.color.ColorPrimary))

Since setTextColor takes an int in parameters. 由于setTextColor采用int参数。

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

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