【问题标题】:Html Content with rgb color not displaying in android but in ios(apple) work perfectly具有 rgb 颜色的 Html 内容未在 android 中显示,但在 ios(apple) 中完美运行
【发布时间】:2020-05-22 15:42:43
【问题描述】:

这是完美的工作

val htmlContent = "<p><span style=\"background-color: #008080;\">Heloo This is new <span style=\"color: #0000ff;\">document</span>&nbsp; <span style=\"background-color: #ffffff;\">&nbsp;TEXTT HHHH<strong>Hhhhhhhh</strong>hhhhhhhhhhhhh</span></span></p>\n" +
                            "<h1><span style=\"background-color: #008080;\"><span style=\"background-color: #ffffff;\">TEst dfsdf&nbsp;</span></span></h1>"

但是当我尝试将其设置在 Html 内容下方时,它在 android 中无法正常工作,但在 ios 中却可以正常工作

<p style="text-align:left;"></p>
<p style="text-align:justify;"><span style="color: rgb(250,197,28);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;"><strong>Yellow </strong></span><span style="color: rgb(0,0,0);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;"><strong>Ipsum</strong></span> <span style="color: rgb(0,0,0);background-color: rgb(255,255,255);font-size: 24px;font-family: Open Sans;">is simply </span><span style="color: rgb(209,72,65);background-color: rgb(255,255,255);font-size: 24px;font-family: Open Sans;">Red</span><span style="color: rgb(209,72,65);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;"> </span><span style="color: rgb(0,0,0);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;">text </span><span style="color: rgb(65,168,95);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;">greenthe </span><span style="color: rgb(0,0,0);background-color: rgb(255,255,255);font-size: 14px;font-family: Open Sans;">printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span></p>
<p style="text-align:left;"><br>&nbsp;</p>

Kotlin 代码

descriptionTextView.text = Html.fromHtml(
                    htmlContent, Html.FROM_HTML_MODE_LEGACY)

【问题讨论】:

    标签: android html ios kotlin textview


    【解决方案1】:

    您应该知道android TextView 不支持所有HTML 标签和属性,并且存在一些限制。有关更多信息,请查看以下问题:
    1。 Which HTML tags are supported by Android TextView?
    2。 Android textview html font size tag

    在您的情况下,不支持以下属性:
    1。文本对齐
    2。字体大小
    3。字体系列
    如果需要设置颜色,rgb(int, int, int) 值也不支持。

    解决方案:
    1。 text-align 属性仅支持 startendcenter 值。
    2。您可以使用&lt;small&gt; 标签来获得较小的字体大小,或者使用&lt;big&gt; 标签获得更大的字体大小。
    3。您可以使用&lt;font&gt; 标签和face 属性来设置字体。
    4。使用十六进制颜色代码(例如 #FF0000)代替 rgb 颜色代码(例如 rgb(255, 0, 0))
    重要提示:如果 html 标记中有多个属性,请使用 ;[Space] 而不是 ; 分隔它们。请看以下示例:

    这个例子不起作用

    <span style="color: #ff0000;background-color: #ffffff;">
        RED
    </span>
    


    但是这个可行

    <span style="color: #ff0000; background-color: #ffffff;">
        RED
    </span>
    

    【讨论】:

      【解决方案2】:

      Android 不支持所有 HTML 标签。

      这取决于 Android 设备中的 HTML 版本。 对于 Batter 输出,您可以使用 web-view 来获得所需的输出

      你可以参考这个blog,这样你就会知道Android支持哪些标签

      【讨论】:

        【解决方案3】:

        您可以尝试在 WebView 中加载该 html 文本

         val encodedHtml: String = Base64.encodeToString(htmlContent.toByteArray(), Base64.NO_PADDING)
         descriptionWebView.loadData(encodedHtml, "text/html; charset=utf-8", "base64");
        

        【讨论】:

        • 在 webView 工作完美,但我不会使用 webView,因为在 120 字符之后我必须添加“阅读更多”标签所以
        猜你喜欢
        • 2022-10-05
        • 2023-03-17
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多