于今腐草无萤火,终古垂杨有暮鸦。这篇文章主要讲述HtmlSpanner 使用小结 -- 安卓解析html相关的知识,希望能为你提供帮助。
安卓解析 html 格式的内容 ( 附: 对于style颜色格式为rgb时的解决办法 )如何利用 HtmlSpanner解析 HTML格式 的字符串:
1. GitHub 下载HtmlSpanner项目 https://github.com/NightWhistler/HtmlSpanner
2. 最好是直接放在java目录下在,这样不需要改引用的包路径
文章图片
3. 引入需要的依赖包
文章图片
implementation \'net.sourceforge.htmlcleaner:htmlcleaner:2.21\' implementation \'com.osbcp:cssparser:1.7\'
4. 使用方法:
文章图片
// 页面上用于展示 html格式的文本布局 TextView textView = findViewById(R.id.htmlSpanner); // 直接 new一个 HtmlSpanner对象 HtmlSpanner htmlSpanner = new HtmlSpanner(); // 格式化 // 解析 html得到 spannable对象 Spannable spannable1 = htmlSpanner.fromHtml("< span style=\'color:red\'> html格式的文字1< /span> "); // 显示到 TextView上 textView.setText(spannable1);
【HtmlSpanner 使用小结 -- 安卓解析html】 5. 在使用中遇到的问题——当富文本中颜色格式是rgb格式,解析失败
文章图片
文章图片
文章图片
解决思路:
1. 首先我们解析的是style=\'color:rgb(0,255,255)\' 这种格式,于是看源码觉得 CSSCompiler 这个类很有问题
2. 找与颜色相关的于是就找到了 parseCSSColor( String colorString ) 这个方法,看起来就是转换颜色用的
3. 源码的写法如下:(是没有对于rgb格式的算法,所以不能解析就很合理啦)
文章图片
4. 想法修改:( 遇到 0rgb格式就先处理成我们的 hex格式,这样不就完美了嘛 )
文章图片
5. 工具类代码如下:
package com.xxx.xxx.xxx; public class ColorUtil {/** * rgb 格式的颜色转 hex格式颜色 * @param rgb * @return */ public static String rgb2hex(String rgb) { int r = 0; int g = 0; int b = 0; int left = rgb.indexOf("("); int right = rgb.indexOf(")"); if (left > -1 & & right > -1 & & right > left) { String substring = rgb.substring(left + 1, right); String[] split = substring.split(","); if (split.length == 3){ r = Integer.valueOf(split[0].trim()); g = Integer.valueOf(split[1].trim()); b = Integer.valueOf(split[2].trim()); } } String rFString, rSString, gFString, gSString, bFString, bSString, result; int red, green, blue; int rred, rgreen, rblue; red = r / 16; rred = r % 16; if (red == 10) rFString = "A"; else if (red == 11) rFString = "B"; else if (red == 12) rFString = "C"; else if (red == 13) rFString = "D"; else if (red == 14) rFString = "E"; else if (red == 15) rFString = "F"; else rFString = String.valueOf(red); if (rred == 10) rSString = "A"; else if (rred == 11) rSString = "B"; else if (rred == 12) rSString = "C"; else if (rred == 13) rSString = "D"; else if (rred == 14) rSString = "E"; else if (rred == 15) rSString = "F"; else rSString = String.valueOf(rred); rFString = rFString + rSString; green = g / 16; rgreen = g % 16; if (green == 10) gFString = "A"; else if (green == 11) gFString = "B"; else if (green == 12) gFString = "C"; else if (green == 13) gFString = "D"; else if (green == 14) gFString = "E"; else if (green == 15) gFString = "F"; else gFString = String.valueOf(green); if (rgreen == 10) gSString = "A"; else if (rgreen == 11) gSString = "B"; else if (rgreen == 12) gSString = "C"; else if (rgreen == 13) gSString = "D"; else if (rgreen == 14) gSString = "E"; else if (rgreen == 15) gSString = "F"; else gSString = String.valueOf(rgreen); gFString = gFString + gSString; blue = b / 16; rblue = b % 16; if (blue == 10) bFString = "A"; else if (blue == 11) bFString = "B"; else if (blue == 12) bFString = "C"; else if (blue == 13) bFString = "D"; else if (blue == 14) bFString = "E"; else if (blue == 15) bFString = "F"; else bFString = String.valueOf(blue); if (rblue == 10) bSString = "A"; else if (rblue == 11) bSString = "B"; else if (rblue == 12) bSString = "C"; else if (rblue == 13) bSString = "D"; else if (rblue == 14) bSString = "E"; else if (rblue == 15) bSString = "F"; else bSString = String.valueOf(rblue); bFString = bFString + bSString; result = "#" + rFString + gFString + bFString; return result; } }
推荐阅读
- 学习之apply,call,bind实现
- Android 线程处理
- 使用AndroidStudio开发cocos2d-x时,可能会出现的问题
- 三维地图app的发展,离不开Infortrend GSe Pro 高效NAS共享
- appium 连接夜神模拟器提示adb版本不匹配的解决方法(adb server version (36) doesn't match this client (41); killing...
- 10—mybatis通用mapper插件 pagehelper 分页
- Centos 中 TCPWrappers访问控制
- create-react-app创建项目后,运行npm run eject报错解决方法
- springboot配置文件 application.yml注意事项(Failed to load property source from location 'classpath:/appl