Android TextView显示Html类解析的网页和图片及自定义标签用法示例
作者:与时俱进 时间:2023-06-12 04:39:41
本文实例讲述了Android TextView显示Html类解析的网页和图片及自定义标签。分享给大家供大家参考,具体如下:
Android系统显示HTML网页的最佳控件为WebView,有时候为了满足特定需求,需要在TextView中显示HTML网页、图片及解析自定义标签。
1、TextView显示Html类解析的网页
CharSequence richText = Html.fromHtml("<strong>萝卜白菜的博客</strong>--<a href='http://orgcent.com'>http://orgcent.com</a>");
mTVText.setText(richText);
//此行必须,否则超链接无法点击,ScrollingMovementMethod实现滚动条
mTVText.setMovementMethod(LinkMovementMethod.getInstance());
PS: 如果想同时让内容可滚动和超链接可点击,只要设置LinkMovementMethod即可。因为其继承了ScrollingMovementMethod。关于ScrollingMovementMethod说明,可查看android实现TextView垂直或水平滚动
2、TextView显示Html解析的图片和自定义标签
final String html = "萝卜白菜的博客<img src='http://m3.img.libdd.com/farm3/115/BBE681F0CAFB16C6806E6AEC1E82D673_64_64.jpg'/><mytag color='blue'>自定义</mytag>";
//处理未知标签,通常是系统默认不能处理的标签
final Html.TagHandler tagHandler = new Html.TagHandler() {
int contentIndex = 0;
/**
* opening : 是否为开始标签
* tag: 标签名称
* output:输出信息,用来保存处理后的信息
* xmlReader: 读取当前标签的信息,如属性等
*/
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if("mytag".equals(tag)) {
if(opening) {//获取当前标签的内容开始位置
contentIndex = output.length();
try {
final String color = (String) xmlReader.getProperty("color");
} catch (Exception e) {
e.printStackTrace();
}
} else {
final int length = output.length();
String content = output.subSequence(contentIndex, length).toString();
SpannableString spanStr = new SpannableString(content);
spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 0, content.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
output.replace(contentIndex, length, spanStr);
}
}
System.out.println("opening:" + opening + ",tag:" + tag + ",output:" + output);
}};
//解析图片
final Html.ImageGetter imageGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
//在此必须异步加载图片
Drawable d = null;
try {
InputStream is = new DefaultHttpClient().execute(new HttpGet(source)).getEntity().getContent();
Bitmap bm = BitmapFactory.decodeStream(is);
d = new BitmapDrawable(bm);
//setBounds(0, 0, bm.getWidth(), bm.getHeight());
d.setBounds(0, 0, 200, 300);
} catch (Exception e) {e.printStackTrace();}
return d;
}
};
richText = Html.fromHtml(html, imageGetter, tagHandler);
mTVText.setText(richText);
希望本文所述对大家Android程序设计有所帮助。
标签:Android,TextView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发之开关按钮控件ToggleButton简单用法示例
2022-12-30 22:05:43
![](https://img.aspxhome.com/file/2023/1/137381_0s.gif)
Springboot全局异常捕获及try catch区别解析
2022-03-02 17:44:07
![](https://img.aspxhome.com/file/2023/7/59227_0s.png)
Android RollPagerView实现轮播图
2023-06-12 21:10:32
java抓取网页或文件中的邮箱号码
2023-07-30 19:19:28
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
Android实现的秒表计时器示例
2023-04-26 11:32:56
Unity Shader实现动态过场切换图片效果
2022-06-30 00:21:43
![](https://img.aspxhome.com/file/2023/0/106370_0s.gif)
详解Android Studio 3.0的新特性与适配
2023-04-29 05:15:05
![](https://img.aspxhome.com/file/2023/1/139111_0s.jpg)
安卓监听屏幕的横竖翻转实现方法
2023-04-01 07:37:12
Spring Cloud Alibaba 整合Nacos的详细使用教程
2022-10-19 22:18:00
![](https://img.aspxhome.com/file/2023/6/119776_0s.png)
Java 使用IO流实现大文件的分割与合并实例详解
2023-08-23 09:33:33
![](https://img.aspxhome.com/file/2023/8/58078_0s.png)
用java WebSocket做一个聊天室
2021-11-30 00:39:55
![](https://img.aspxhome.com/file/2023/2/60392_0s.jpg)
Base64编码解码原理及C#编程实例
2022-05-07 03:58:53
![](https://img.aspxhome.com/file/2023/8/70438_0s.png)
Java Pattern和Matcher字符匹配方式
2022-06-07 21:57:56
![](https://img.aspxhome.com/file/2023/5/93075_0s.png)
Opencv光流运动物体追踪详解
2023-06-21 11:55:31
![](https://img.aspxhome.com/file/2023/4/91994_0s.jpg)
手动模拟JDK动态代理的方法
2023-03-23 00:49:03
Java框架解说之BIO NIO AIO不同IO模型演进之路
2021-06-20 22:56:31
![](https://img.aspxhome.com/file/2023/6/75026_0s.png)
Android编程设计模式之中介者模式详解
2022-04-05 21:13:03
![](https://img.aspxhome.com/file/2023/9/139009_0s.png)
Android实现截图和分享功能的代码
2023-04-09 22:56:43
![](https://img.aspxhome.com/file/2023/8/106868_0s.png)
详解Unity 实现语音识别功能
2023-11-30 22:01:11
![](https://img.aspxhome.com/file/2023/3/119553_0s.gif)
使用idea+gradle编译spring5.x.x源码分析
2022-05-13 15:13:31
![](https://img.aspxhome.com/file/2023/3/83453_0s.jpg)