Andoid TextView显示富文本html内容及问题处理

目录

  • 富文本内容与效果
  • TextView + Html
  • ImageGetter 处理图片(表情)
  • TagHandler 处理html内容的节点
  • Html的转换过程
    • HtmlToSpannedConverter
      • handleStartTag
      • startCssStyle(mSpannableStringBuilder, attributes)字体无效果实现
      • getForegroundColorPattern颜色不显示的坑
  • 处理办法
    • 颜色修改
    • 粗体支持
    • 斜体支持

来了来了,
html页面内容不是用用webview的吗,TextView显示html什么鬼?
老铁莫急,没错,html页面内容多数情况下都是用webview来显示的,尤其是app里面常见的"关于"、“隐私政策”等,这都是单一的显示,或者整个页面就显示这么一个page页面,自然也就选择webview。

当遇到富文本这样的html内容片段,而且是以列表方式显示多段内容不一样的内容时候,怎么办呢。基于源生的习惯,自然就是TextView + Html了。
有坑,但问题不大。

富文本内容与效果

如下一段html,粗体、斜体、橘色和带了一个表情:

<SPAN style="FONT-SIZE: 10pt; FONT-WEIGHT: bold; COLOR: #ff8000; FONT-STYLE: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy
</SPAN>

效果:
在这里插入图片描述

TextView + Html

TextView 就不介绍了,主要介绍Html这个类。
Html.java在android.text这个package下,包名也就看出是和文字相关的类。其核心本质是解析html内容,根据html的style给构造出Spanned,Spanned又是什么东西?Spanned是CharSequence的孩子。
平常给TextView设置文字 setText(CharSequence text)这个函数的参数就是CharSequence ,只不过实际传递的是CharSequence 的另外一个孩子String。

Html.java 提供html内容和Spanned的相互转换:
html->Spanned:

public static Spanned fromHtml(String source, int flags)
public static Spanned fromHtml(String source, int flags, ImageGetter imageGetter,TagHandler tagHandler)

Spanned->html:

public static String toHtml(Spanned text, int option)

但实际上上述内容出表情外,其他的效果完全没有,包括颜色。具体请往后看

ImageGetter 处理图片(表情)

当遇到<img>标签的时候就会回调, 对应的返回一个Drawable对象。source 参数就是<img>的src属性的内容,也就是图片路径。根据上文给出的内容,这里source是“emotion\emotion.rb.gif”。同时注意返回的Drawable对象一定要给定边界,也就是drawable.setBounds(),不然不会显示图片。如果这里返回一个null,一般出现一个小矩形。

/*** Retrieves images for HTML &lt;img&gt; tags.*/public static interface ImageGetter {/*** This method is called when the HTML parser encounters an* &lt;img&gt; tag.  The <code>source</code> argument is the* string from the "src" attribute; the return value should be* a Drawable representation of the image or <code>null</code>* for a generic replacement image.  Make sure you call* setBounds() on your Drawable if it doesn't already have* its bounds set.*/public Drawable getDrawable(String source);}

TagHandler 处理html内容的节点

这个标签捕获是有条件的,如果html内容的标签没有在Html中定义捕才回调出来,在显示效果上是没效的,需要自行处理这个标签,对应的编辑output。

 /*** Is notified when HTML tags are encountered that the parser does* not know how to interpret.*/public static interface TagHandler {/*** This method will be called whenn the HTML parser encounters* a tag that it does not know how to interpret.*/public void handleTag(boolean opening, String tag,Editable output, XMLReader xmlReader);}

注释讲的清楚,parser 识别不了的就会回调通知。

Html的转换过程

从fromHtml入口可以看出,是HtmlToSpannedConverter 在工作,执行convert

    public static Spanned fromHtml(String source, int flags, ImageGetter imageGetter,TagHandler tagHandler) {Parser parser = new Parser();try {parser.setProperty(Parser.schemaProperty, HtmlParser.schema);} catch (org.xml.sax.SAXNotRecognizedException e) {// Should not happen.throw new RuntimeException(e);} catch (org.xml.sax.SAXNotSupportedException e) {// Should not happen.throw new RuntimeException(e);}HtmlToSpannedConverter converter =new HtmlToSpannedConverter(source, imageGetter, tagHandler, parser, flags);return converter.convert();}

HtmlToSpannedConverter

从代码上看HtmlToSpannedConverter 还不是内部类,是和Html平行定义的。且实现了ContentHandler,ContentHandler就是xml解析回调接口,而该类主要处理了3个回调,其余空实现:

public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {handleStartTag(localName, attributes);}public void endElement(String uri, String localName, String qName) throws SAXException {handleEndTag(localName);}public void characters(char ch[], int start, int length) throws SAXException {StringBuilder sb = new StringBuilder();/** Ignore whitespace that immediately follows other whitespace;* newlines count as spaces.*/for (int i = 0; i < length; i++) {char c = ch[i + start];if (c == ' ' || c == '\n') {char pred;int len = sb.length();if (len == 0) {len = mSpannableStringBuilder.length();if (len == 0) {pred = '\n';} else {pred = mSpannableStringBuilder.charAt(len - 1);}} else {pred = sb.charAt(len - 1);}if (pred != ' ' && pred != '\n') {sb.append(' ');}} else {sb.append(c);}}mSpannableStringBuilder.append(sb);}

当开始一个标签时调用 handleStartTag
结束一个标签时调用handleEndTag
解析到文本的时候就追加到mSpannableStringBuilder

handleStartTag

这里能看出Html类处理了多少标签,同时也应证没有定义的标签都抛给外部处理
注意这里标签的匹配不区分大小写 (equalsIgnoreCase)

private void handleStartTag(String tag, Attributes attributes) {if (tag.equalsIgnoreCase("br")) {// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>// so we can safely emit the linebreaks when we handle the close tag.} else if (tag.equalsIgnoreCase("p")) {startBlockElement(mSpannableStringBuilder, attributes, getMarginParagraph());startCssStyle(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("ul")) {startBlockElement(mSpannableStringBuilder, attributes, getMarginList());} else if (tag.equalsIgnoreCase("li")) {startLi(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("div")) {startBlockElement(mSpannableStringBuilder, attributes, getMarginDiv());} else if (tag.equalsIgnoreCase("span")) {startCssStyle(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("strong")) {start(mSpannableStringBuilder, new Bold());} else if (tag.equalsIgnoreCase("b")) {start(mSpannableStringBuilder, new Bold());} else if (tag.equalsIgnoreCase("em")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("cite")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("dfn")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("i")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("big")) {start(mSpannableStringBuilder, new Big());} else if (tag.equalsIgnoreCase("small")) {start(mSpannableStringBuilder, new Small());} else if (tag.equalsIgnoreCase("font")) {startFont(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("blockquote")) {startBlockquote(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("tt")) {start(mSpannableStringBuilder, new Monospace());} else if (tag.equalsIgnoreCase("a")) {startA(mSpannableStringBuilder, attributes);} else if (tag.equalsIgnoreCase("u")) {start(mSpannableStringBuilder, new Underline());} else if (tag.equalsIgnoreCase("del")) {start(mSpannableStringBuilder, new Strikethrough());} else if (tag.equalsIgnoreCase("s")) {start(mSpannableStringBuilder, new Strikethrough());} else if (tag.equalsIgnoreCase("strike")) {start(mSpannableStringBuilder, new Strikethrough());} else if (tag.equalsIgnoreCase("sup")) {start(mSpannableStringBuilder, new Super());} else if (tag.equalsIgnoreCase("sub")) {start(mSpannableStringBuilder, new Sub());} else if (tag.length() == 2 &&Character.toLowerCase(tag.charAt(0)) == 'h' &&tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {startHeading(mSpannableStringBuilder, attributes, tag.charAt(1) - '1');} else if (tag.equalsIgnoreCase("img")) {startImg(mSpannableStringBuilder, attributes, mImageGetter);} else if (mTagHandler != null) {//除以上标签以外,都回调给外部处理mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);}}

明明<SPAN> 是被解析的(tag.equalsIgnoreCase(“span”),就是没有颜色和粗体、斜体呢?再看startCssStyle(mSpannableStringBuilder, attributes)

startCssStyle(mSpannableStringBuilder, attributes)字体无效果实现

private void startCssStyle(Editable text, Attributes attributes) {String style = attributes.getValue("", "style");if (style != null) {Matcher m = getForegroundColorPattern().matcher(style);if (m.find()) {int c = getHtmlColor(m.group(1));if (c != -1) {start(text, new Foreground(c | 0xFF000000));}}m = getBackgroundColorPattern().matcher(style);if (m.find()) {int c = getHtmlColor(m.group(1));if (c != -1) {start(text, new Background(c | 0xFF000000));}}m = getTextDecorationPattern().matcher(style);if (m.find()) {String textDecoration = m.group(1);if (textDecoration.equalsIgnoreCase("line-through")) {start(text, new Strikethrough());}}}}

取出style 属性,且此处指处理颜色,并没有处理字体,字体肯定是不会有效果的了。接着看getForegroundColorPattern。

getForegroundColorPattern颜色不显示的坑

再看取属性里面的颜色是通过正则表达式来取的,前景色正则表达式: “(?:\s+|\A)color\s*:\s*(\S*)\b”)

private static Pattern getForegroundColorPattern() {if (sForegroundColorPattern == null) {sForegroundColorPattern = Pattern.compile("(?:\\s+|\\A)color\\s*:\\s*(\\S*)\\b");}return sForegroundColorPattern;}

这里是个坑,color是小写,内容中的是COLOR,没有匹配到颜色。同时背景色也是小写的color。

处理办法

调用还是不变,但要对原始内容进行修改

颜色修改

直接将style 属性的值修改为小写

<!--这样就可以显示颜色了-->
<SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN>

粗体支持

从代码上看是明确不处理style 属性中的粗体,但从handleStartTag解析中有如下片段

 else if (tag.equalsIgnoreCase("strong")) {start(mSpannableStringBuilder, new Bold());} else if (tag.equalsIgnoreCase("b")) {start(mSpannableStringBuilder, new Bold());}

基于这个片段,判断style中属性中的font-weight如果是bold值,那么直接在原内容基础上包裹标签<strong>或<b>。
具体如下:

<b><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN>
</b>//或
<strong><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN>
</strong>

斜体支持

思路和粗体一样,选择多一点
代码片段:

else if (tag.equalsIgnoreCase("em")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("cite")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("dfn")) {start(mSpannableStringBuilder, new Italic());} else if (tag.equalsIgnoreCase("i")) {start(mSpannableStringBuilder, new Italic());}

基于这个片段,判断style中属性中的font-style如果是italic值,那么直接在原内容基础上包裹标签<em>,<cite>,<dfn>,<i>之一
具体如下:

<em><b><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN></b>
</em>
//或
<cite><b><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN></b>
</cite>//或
<dfn><b><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN></b>
</dfn>
//或
<i><b><SPAN style="font-size: 10pt; font-weight: bold; color: #ff8000; font-style: italic">hello<IMG src="emotion\emotion.rb.gif" thePath custom="false">boy</SPAN></b>
</i>

至此,这段html的颜色、粗体、斜体都能显示了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558206.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql截取字符串最后两位_MySQL字符串函数substring:字符串截取

MySQL 字符串截取函数&#xff1a;left(), right(), substring(), substring_index()。还有 mid(), substr()。其中&#xff0c;mid(), substr() 等价于 substring() 函数&#xff0c;substring() 的功能非常强大和灵活。1. 字符串截取&#xff1a;left(str, length)mysql> …

Java类加载顺序之一条日志引发的血案

目录为什么是null回顾类加载原因问题重现总结类加载顺序子类重写被父类构造函数调用的函数注意不能放过不起眼的日志一条日志引发的案子 [11:12:58.505][D][Gen][RTLive][getIns ins 4414717] [11:12:58.774][I][Gen][null][updateShowMode] [11:12:58.864][D][Gen][VideoCame…

Java错误:找不到或无法加载主类

目录前言javac xxx.java 编译需要相对物理路径java xxx 执行需要虚拟路径总结前言 一般情况下&#xff0c;我们都使用工具进行代码的编辑和调试&#xff0c;例如eclipse 、Manven、Android Studio、sublime、vim、notepad、记事本等。 当我们用eclipse android studio等创建的p…

vue取通过key取value_如何通过获取map中的key来获得与key对应的value值,进行运算...

展开全部获取map的key和value的方法分为以下62616964757a686964616fe4b893e5b19e31333366306439两种形式&#xff1a;1、map.keySet()&#xff1a;先获取map的key&#xff0c;然后根据key获取对应的value&#xff1b;2、map.entrySet()&#xff1a;同时查询map的key和value&…

Android坑点-ByteBuffer.array() 入过坑吗

目录1、坑点介绍2、正确使用姿势&#xff08;入坑了怎么办&#xff09;3、坑坑详解3.1HeapByteBuffer可以用buffer.array()3.2DirectByteBuffer的坑在哪里1、坑点介绍 如下代码&#xff1a; ByteBuffer buffer ByteBuffer.allocateDirect(int capacity) byte[] array buf…

php四则运算出题器_php实现简单四则运算器

本文实例为大家分享了php实现简单四则运算器的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下前端代码 &#xff1a;php计算器请输入num1&#xff1a;请选择运算符&#xff1a;-*/%请输入num2&#xff1a;后台&#xff1a;php计算器$num1$_post["num1"];$n…

步苹果iOS的后尘,谷歌Android12“翻车”,更新需谨慎?

苹果不论电脑还是移动设备&#xff0c;都是一如既往的“强硬”。说实话&#xff0c;忽视“兼容”&#xff0c;体验极低。 有小伙伴调侃&#xff1a;人家就是要你买新机器。也有小伙伴&#xff08;包括我在内&#xff09;&#xff0c;直接关闭系统自动更新。 开发者&#xff1a…

word把选择答案弄到题目里_word中把选择题的正确答案自动填到括号里技巧

为了适应各类复习迎考&#xff0c;大家都会利用一些题目来练习。当面对题目与答案分离的现状(两个文档或一个文档的两个部分)时&#xff0c;怎样将题目和答案合二为一&#xff0c;使答案自动填充到题目后的括号中是颇让大家头疼的一件事情。经过实践探索多步骤的组合操作可以实…

Android Studio无线连接设备调试,比数据线更方便

前言 一般情况下&#xff0c;多数移动开发者使用的是数据线连接电脑&#xff0c;进行各种移动设备的调试&#xff0c;更有胜者&#xff0c;非常迷恋模拟器&#xff0c;模拟器它好不好&#xff0c;答案是好&#xff0c;因为直接运行在电脑上&#xff0c;直接操作&#xff0c;调试…

机器学习里面的基函数_神经网络与机器学习 笔记—核方法和径向基函数网络(上)...

对于神经网络的监督学习有多种不同方法。之前一直总结整理的多层感知器反向传播算法&#xff0c;可以看做是递归技术的应用&#xff0c;这种技术在统计学中通称为随机逼近。这次是采用不同的途径&#xff0c;通过包含如下两个阶段的混合方式解决非线性可分模式的分类问题&#…

AndroidJava List与equals的微妙关系,小心掉坑里

前言 List 有多个实现&#xff0c;本文以ArrayList(LinkedList也一样)作为说明&#xff0c;equals是Object的一个成员函数&#xff0c;例子中的bean重写实现它。 一、Bean 类定义并重写equals函数 public class Book {private String id;private String name;public String ge…

apache缺省banner_http服务器缺省banner

HTTP协议详解(真的很经典)HTTP协议详解(真的很经典)Author :清晨引言HTTP是一个属于应用层的面向对象的协议&#xff0c;由于其简捷、快速的方式&#xff0c;适用于分布式超媒体信息系统。它于1990年提出&#xff0c;经过几年的使用与发展&#xff0c;得到不断地完善和扩展。目…

GenseeSDK 使用Kotlin要注意TODOAndroid Studio关闭TODO

目录一、Kotlin的TODO二、GenseeSDK与TODO 请注意三、Android studio关闭TODO一、Kotlin的TODO 在实现一些接口时候&#xff0c;工具自动将所有的接口函数"空"实现&#xff0c;并在函数体中增加一行代码&#xff1a; TODO或TODO(“not implemented”) 作为提醒催促…

如何启动netcat_Netcat用法

Netcat用法--服务泄漏内部信息要得到某些端口号的详细信息&#xff0c;可以连接到某个端口&#xff0c;对应的服务会告知它的版本号、结构甚至其工作的操作系统。所以&#xff0c;可以使用Netcat来扫描一个特定范围内的端口并报告在这些端口上运行的服务。要使Netcat自动工作&a…

AndroidJava try-catch-finally正确用法

目录一、try-catch-finally的用途二、try-catch的正确使用三、奇怪的try-finally &#xff08;错误的用法&#xff09;四、关于try-catch-finally的面试考察1、try、catch、finally 考察&#xff0c;请指出下面程序的运行结果。2、try、catch、finally 考察2&#xff0c;请指出…

python tcp服务器并发_python tcp并发服务器

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":6,"count":6}]},"card":[{"des":"云服务器 ECS(Elastic Compute Service)是一…

OpenCV Mat基础认知感

OpenCV是一个开源的供开发的计算机视觉处理库&#xff0c;涵盖的内容包括图像处理&#xff0c;机器学习。由c到c &#xff0c;再到各平台的跨平台框架。 Mat - 图像容器 Mat类用于表示一个多维的单通道或者多通道的稠密数组。能够用来保存实数或复数的向量、矩阵&#xff0c;…

layui多文件上传讲解_Layui 多文件上传 SSH

jsp 页面pageEncoding"UTF-8"%>Insert title here选择多文件文件名大小状态操作开始上传layui.use(upload, function(){var $ layui.jquery,upload layui.upload;//多文件列表示例var demoListView $(#demoList),uploadListIns upload.render({elem: #testLis…

OpenCV:H1.type() == H2.type() H1.depth() == CV_32F

如题&#xff1a; E/cv::error(): OpenCV(4.1.0) Error: Assertion failed (H1.type() H2.type() && H1.depth() CV_32F) in compareHist, file /build/master_pack-android/opencv/modules/imgproc/src/histogram.cpp, line 1936 E/org.opencv.imgproc: imgproc::co…

css 外弧_css 伪类实现弧形

在实现页面五花八门的有特色的ui时&#xff0c;我们有时会遇到要用实现一个弧形&#xff0c;而这样的弧形要怎么实现呢&#xff1f;用图片&#xff1f;不太优雅&#xff0c;这样就要无故多加载一张图片了&#xff0c;这里来说说怎么用css的after伪类来实现弧形。总思路&#xf…