这是我将字符串转换为颜色的代码:
public Color prepareColour(String str) {
str.replace("#", "");
float r = Float.valueOf(str.substring(0,1));
float g = Float.valueOf(str.substring(2,3));
float b = Float.valueOf(str.substring(4,5));
Color color = Color.valueOf(r,g,b);
return color;
}
我收到以下调试错误:
Error:(16, 23) error: constructor Color in class Color cannot be applied to given types; required: no arguments found: float,float,float reason: actual and formal argument lists differ in length
但是,从Android Studio编译前的建议是:
Call requires API level 26 (current min is 17) ......
我看到 2011 年的答案支持这种创建颜色的方式,所以它肯定适用于 API 17 而不需要 26。
我已经尝试清理和重建项目,以及用实际值替换 str.substring
并且没有任何变化。
为什么代码编译不通过?
最佳答案
您是否尝试过使用 Color.rgb(r,g,b)
而不是 Color.valueOf(...)
? Color.valueOf(...)
是 Android Developer O 预览版中的一个非常新的方法,因此目前它仅在 1 个 API 级别上有用。
此外,请确保您使用的是 0-255 范围内的整数,或 0-1 范围内的 float 。
关于java - 在 Java 中从 rgb 字符串创建颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783591/