【问题标题】:How to convert HTML String with RGB colors to HTML String with HEX colors in Android JAVA如何在 Android JAVA 中将具有 RGB 颜色的 HTML 字符串转换为具有 HEX 颜色的 HTML 字符串
【发布时间】:2019-02-09 21:17:04
【问题描述】:

我有以下 html 文本(示例):

<p>​<span style="color: rgb(33, 150, 243);">This is a <span style="background-color: rgb(255, 235, 59);">test !!</span></span></p>

我正在尝试在 TextView 中显示此文本。

textview.setText(Html.fromHtml(string));

它可以工作,但不显示颜色。

为此,我必须以这种形式转换文本(使用十六进制颜色)

<p>​<span style="color: #2196f3;">This is a <span style="background-color: #ffeb3b;">test !!</span></span></p>

我该怎么做?

我找不到解决方案...有没有人遇到过这个问题并可以帮助我?

提前致谢!

【问题讨论】:

  • 您的代码中是否有单独的颜色变量 (int r; int g; int b;) 或者您所拥有的一切都是这个 HTML 字符串,并且您想从这个字符串中解析出值?跨度>
  • 一切都在字符串中。我从远程服务器获取字符串,并尝试在文本视图中正确显示它。

标签: java android html colors fromhtml


【解决方案1】:

cmets 中的解释:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Stackoverflow52173062 {

    public static void main(final String[] args) {

        String html = "<p>​<span style=\"color: rgb(33, 150, 243);\">This is a <span style=\"background-color: rgb(255, 235, 59);\">test !!</span></span></p>";

        html = replaceRGBColorsWithHex(html);

        // final String
        System.out.println(html);
    }

    private static String replaceRGBColorsWithHex(String html) {
        // using regular expression to find all occurences of rgb(a,b,c) using
        // capturing groups to get separate numbers.
        Pattern p = Pattern.compile("(rgb\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\))");
        Matcher m = p.matcher(html);

        while (m.find()) {
            // get whole matched rgb(a,b,c) text
            String foundRGBColor = m.group(1);
            System.out.println("Found: " + foundRGBColor);

            // get r value
            String rString = m.group(2);
            // get g value
            String gString = m.group(3);
            // get b value
            String bString = m.group(4);

            System.out.println(" separated r value: " + rString);
            System.out.println(" separated g value: " + gString);
            System.out.println(" separated b value: " + bString);

            // converting numbers from string to int
            int rInt = Integer.parseInt(rString);
            int gInt = Integer.parseInt(gString);
            int bInt = Integer.parseInt(bString);

            // converting int to hex value
            String rHex = Integer.toHexString(rInt);
            String gHex = Integer.toHexString(gInt);
            String bHex = Integer.toHexString(bInt);

            // add leading zero if number is small to avoid converting
            // rgb(1,2,3) to rgb(#123)
            String rHexFormatted = String.format("%2s", rHex).replace(" ", "0");
            String gHexFormatted = String.format("%2s", gHex).replace(" ", "0");
            String bHexFormatted = String.format("%2s", bHex).replace(" ", "0");

            System.out.println(" converted " + rString + " to hex: " + rHexFormatted);
            System.out.println(" converted " + gString + " to hex: " + gHexFormatted);
            System.out.println(" converted " + bString + " to hex:" + bHexFormatted);

            // concatenate new color in hex
            String hexColorString = "#" + rHexFormatted + gHexFormatted + bHexFormatted ;

            System.out.println("  replacing " + foundRGBColor + " with " + hexColorString);
            html = html.replaceAll(Pattern.quote(foundRGBColor), hexColorString);
        }
        return html;
    }
}

【讨论】:

  • 完美运行,只需编辑以下行: // 以十六进制连接新颜色 String hexColorString = "rgb(#" + rHexFormatted + gHexFormatted + bHexFormatted + ")";至:字符串 hexColorString = "#" + rHexFormatted + gHexFormatted + bHexFormatted;谢谢:-)
  • 出于好奇,能否实现相反的功能? (十六进制转 RGB)
  • 是的。这将需要或多或少地扭转所有这些操作。您应该首先将“#rrggbb”拆分为单独的字符串:rr、gg、bb,然后使用 Integer.parseInt(rr, 16); 将它们中的每一个转换为十进制
  • 我明白你的意思,但我不太明白如何在字符串中找到颜色。只有当颜色与字符串的其余部分隔离时,我才能转换。如果您有解决方案,您也可以发布它来回答问题。对未来的读者来说,这个主题将是完整的 :-) '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2012-02-06
  • 2012-11-01
  • 2019-02-08
  • 2019-01-01
  • 2011-02-20
  • 2011-05-12
相关资源
最近更新 更多