简体   繁体   中英

Android Html.fromHtml(span) is not working for mark tag

  • Method

     public Spanned fromHtml(String html) { Spanned result; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY); } else { // This method is deprecated but need to use for old os devices return Html.fromHtml(html); } return result; } 

input

String description = "Normal text <mark style=\"font-weight:600;color:#000;\">highlight this text</mark> not working";

It is showing normal text on TextView .

It seems Html.Java doesn't contain mark tag. Is there any workaround for this?

Please don't suggest spannable string with start index and end index because string lengh is dynamic one.

Is there any workaround for this?

Option #1: Change the text to use a supported tag at its source, to avoid the use of <mark>

Option #2: Create your own parser that can handle this tag, identifying the start and end positions, and creating a Spanned using SpannableStringBuilder , the way that Html does

Option #3: Use a regex to try to replace the <mark> and </mark> with some supported tag, then parse that modified string with Html

Option #4: Use WebView to render this markup

Option #5: Use a regex to try to eliminate the <mark> and </mark> tags, so you can just use the plain string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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