Html RGB 颜色的内容在 android 中不显示,但在 ios(苹果)中工作完美
Html Content with rgb color not displaying in android but in ios(apple) work perfectly
这是完美的作品
val htmlContent = "<p><span style=\"background-color: #008080;\">Heloo This is new <span style=\"color: #0000ff;\">document</span> <span style=\"background-color: #ffffff;\"> TEXTT HHHH<strong>Hhhhhhhh</strong>hhhhhhhhhhhhh</span></span></p>\n" +
"<h1><span style=\"background-color: #008080;\"><span style=\"background-color: #ffffff;\">TEst dfsdf </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> </p>
Kotlin 代码
descriptionTextView.text = Html.fromHtml(
htmlContent, Html.FROM_HTML_MODE_LEGACY)
您可以尝试在 WebView 中加载该 html 文本
val encodedHtml: String = Base64.encodeToString(htmlContent.toByteArray(), Base64.NO_PADDING)
descriptionWebView.loadData(encodedHtml, "text/html; charset=utf-8", "base64");
您应该知道 HTML 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
属性仅支持 start
、end
和 center
值。
2。您可以使用 <small>
标记较小的字体大小或 <big>
标记较大的字体大小。
3。您可以使用 <font>
标签和 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>
Android 不支持所有 HTML 标签。
这取决于 Android 设备中的 HTML 版本。
对于 Batter 输出,您可以使用 web-view 来获得所需的输出
您可以参考这个 blog 这样您就会了解 Android
支持哪些标签
这是完美的作品
val htmlContent = "<p><span style=\"background-color: #008080;\">Heloo This is new <span style=\"color: #0000ff;\">document</span> <span style=\"background-color: #ffffff;\"> TEXTT HHHH<strong>Hhhhhhhh</strong>hhhhhhhhhhhhh</span></span></p>\n" +
"<h1><span style=\"background-color: #008080;\"><span style=\"background-color: #ffffff;\">TEst dfsdf </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> </p>
Kotlin 代码
descriptionTextView.text = Html.fromHtml(
htmlContent, Html.FROM_HTML_MODE_LEGACY)
您可以尝试在 WebView 中加载该 html 文本
val encodedHtml: String = Base64.encodeToString(htmlContent.toByteArray(), Base64.NO_PADDING)
descriptionWebView.loadData(encodedHtml, "text/html; charset=utf-8", "base64");
您应该知道 HTML 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
属性仅支持 start
、end
和 center
值。
2。您可以使用 <small>
标记较小的字体大小或 <big>
标记较大的字体大小。
3。您可以使用 <font>
标签和 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>
Android 不支持所有 HTML 标签。
这取决于 Android 设备中的 HTML 版本。 对于 Batter 输出,您可以使用 web-view 来获得所需的输出
您可以参考这个 blog 这样您就会了解 Android
支持哪些标签