Show
  • in android textview

前端 未结 7 570
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 16:15

I have String with ul and li in it. And I am trying to show them in HTML formatting in textview.
textView.setText(Html.fromHtml(myHtmlText));
But tex

7条回答
  •  没有蜡笔的小新
    2020-12-14 16:45

    A really easy way to make things work for API level M (23) and lower From your HTML string you can use the following:

    private fun String.formattedHtml(): String {
        // Work around for API Versions lower than N, Html.fromHtml does not render list items correctly
        return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            this.replace("
      ", "") .replace("
    ", "") .replace("
  • ", "

    • ") .replace("

  • ", "

    ") } else { this } }

    Then, you can use:

    Html.fromHtml("
    • test
    ".formattedHtml())

    No need for TagHandler for API N and up, as mentioned in https://stackoverflow.com/a/40524835, android.text.Html supports li and ul tags.

提交回复
热议问题