[英]how to transform from string to Color android
I need to convert an string to Color my api response is this "mainColor": "#8439FF"
, i need to transformit to Color我需要将字符串转换为颜色我的 api 响应是这个
"mainColor": "#8439FF"
,我需要将它转换为颜色
i have tried the next solutions我已经尝试了下一个解决方案
int color = Color.parseColor(mainColor);
val string = mainColor
val ColorPrimario = string.replaceFirst("^#".toRegex(), "").toInt(16)
the problem whit this solutions is that the result its an int not Color, and it marks error for that reason.这个解决方案的问题是它的结果是int而不是Color,因此它标记了错误。 any help would be appreciated
任何帮助,将不胜感激
-------------Edit------------- - - - - - - -编辑 - - - - - - -
I have solved the problem i did it this way我已经解决了我这样做的问题
val string = mainColor
val color = Color(string.toColorInt())
Try this:尝试这个:
val color: Color = Color.valueOf(Color.parseColor(mainColor))
See https://developer.android.com/reference/android/graphics/Color#valueOf(int)请参阅https://developer.android.com/reference/android/graphics/Color#valueOf(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.