I have html text that I need to display in TextView. The html may look like this -

<font color="#AFEEEE"><font style="background-color: rgb(255,140,0);">Text with background and color</font></font>

Html.fromHtml doesn't support any attribute other than color for font tag. But we absolutely must show the background. I could write a custom tag handler but the attributes are not passed in, only the tag is passed in. What is the best way to achieve this ?

NOTE : Cant use Webview.


I tried the code below. If I set raw on the text, it works, but if i process it further and pass it to Html.fromHtml, it doesnt show the background.

public static final String sText =
    "Background on <font style=\"background-color: rgb(255,255,0);\">pa</font>rt text only";

        Pattern pattern = Pattern.compile(BACKGROUND_PATTERN);
        Matcher matcher = pattern.matcher(sText);
        SpannableString raw = new SpannableString(sText);
        BackgroundColorSpan[] spans =
            raw.getSpans(0, raw.length(), BackgroundColorSpan.class);
        for (BackgroundColorSpan span : spans) {
            raw.removeSpan(span);
        }

        while (matcher.find()) {
            raw.setSpan(new BackgroundColorSpan(0xFF8B008B),
                matcher.start(2), matcher.start(2) + matcher.group(2).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        sText = raw.toString();
        final Spanned convertedHtml =
            Html.fromHtml(sText, ig, new myTagHandler());
有帮助吗?

解决方案 2

Add your own BackgroundColorSpan as you see fit.

Here is some code that sets such a span on all occurrences of a search term within a TextView:

  private void searchFor(String text) {
    TextView prose=(TextView)findViewById(R.id.prose);
    Spannable raw=new SpannableString(prose.getText());
    BackgroundColorSpan[] spans=raw.getSpans(0,
                                             raw.length(),
                                             BackgroundColorSpan.class);

    for (BackgroundColorSpan span : spans) {
      raw.removeSpan(span);
    }

    int index=TextUtils.indexOf(raw, text);

    while (index >= 0) {
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
          + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }

    prose.setText(raw);
  }

So, find your beginning and ending points, create a BackgroundSpan with your desired color, and use setSpan() to apply it.

Note that this assumes that only part of your text needs the background color. If the entire TextView needs the color, go with njzk2's suggestion, and just apply the color to the whole TextView.

其他提示

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    String str = "<span style=\"background-color:#f3f402;\">" + TEXT TO HIGHLIGHT + "</span>";
    textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY));
} else {
    String str = "<font color='#f3f402'>" + TEXT TO HIGHLIGHT + "</font>";
    textView.setText(Html.fromHtml(str));
}

More - https://stackoverflow.com/a/46035856/3625510

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top