[英]Android: How to Convert String RGB Value to Color to use in View
我從數據庫中獲得以下字符串值: "rgb(105, 105, 105)"
我嘗試了Color.parseColor()
但這不是正確的選擇。
我嘗試將其與view.setBackgroundColor()
一起使用
在Java / Android中有沒有辦法解決此問題?
對於"rgb(105, 105, 105)"
這種格式,您必須手動解析。 試試這個代碼:
try{
String str = "rgb(105, 105, 105)";
String splitStr = str.substring(str.indexOf('(') + 1, str.indexOf(')'));
splitString = splitStr.split(",");
int colorValues[] = new int[splitString.length];
for (int i = 0; i < splitString.length; i++) {
colorValues[i] = Integer.parseInt(splitString[i].trim());
}
int color = Color.rgb(colorValues[0], colorValues[1],colorValues[2]);
view.setBackgroundColor(color);
}catch(Exception ex){
}
簡短:
view.setBackgroundColor(Color.rgb(105, 105, 105));
編輯:阿布先生已經給出了解析的答案。 它的完整答案,使用它。 我會放棄我的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.