简体   繁体   English

Android:如何将字符串RGB值转换为颜色以在视图中使用

[英]Android: How to Convert String RGB Value to Color to use in View

I get the following string value from the db: "rgb(105, 105, 105)" 我从数据库中获得以下字符串值: "rgb(105, 105, 105)"

I tried Color.parseColor() but it wasn't right option. 我尝试了Color.parseColor()但这不是正确的选择。

I try to use it with view.setBackgroundColor() 我尝试将其与view.setBackgroundColor()一起使用

Is there a way for this in Java/Android? 在Java / Android中有没有办法解决此问题?

For "rgb(105, 105, 105)" this format you have to parse it manually. 对于"rgb(105, 105, 105)"这种格式,您必须手动解析。 Try this code: 试试这个代码:

  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){

 }

Short and Simple: 简短:

view.setBackgroundColor(Color.rgb(105, 105, 105));

Edit: Mr Abu has given answer with parsing. 编辑:阿布先生已经给出了解析的答案。 Its complete answer, use it. 它的完整答案,使用它。 I will discard my answer. 我会放弃我的答案。

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

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