展会信息港展会大全

Android TextView中显示HTML和图片
来源:互联网   发布日期:2016-01-14 09:46:27   浏览:2893次  

导读:最近有人咨询我如何在Android TextView中显示img src=/ html标签内的图片,大家都知道,在TextView中显示HTML内容的方法如下所示:TextView description=(TextView)findViewById(R.id.description);desc......

最近有人咨询我如何在Android TextView中显示<img src=""/> html标签内的图片,大家都知道,在TextView中显示HTML内容的方法如下所示:

TextView description=(TextView)findViewById(R.id.description);

description.setText(Html.fromHtml(item.getDescription()));

如果HTML中有图片的话,显示出来的图片会被一个小框取代,那么怎么样才能看到图片呢?查看了一下API,android.text.Html还还有另一个方法:Html.fromHtml(String source,ImageGetter imageGetter,TagHandler tagHandler),这个方法使用如下所示:

ImageGetter imgGetter = new Html.ImageGetter() {

public Drawable getDrawable(String source) {

Drawable drawable = null;

Log.d("Image Path", source);

URL url;

try {

url = new URL(source);

drawable = Drawable.createFromStream(url.openStream(), "");

}

catch (Exception e) {

return null;

}

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

return drawable;

}

};

.........

TextView description=(TextView)findViewById(R.id.description);

description.setText(Html.fromHtml(item.getDescription(),imgGetter,null));

第二个参数TagHandler是处理HTML中的标签的,比如说遇到某个标签就把它替换为 .之类的操作都可以通过TagHandler来处理,呵呵,我可没试过哦,瞎猜的,程序员一定要发挥充分的想像力,自己去试一下吧!

最后我要说的是,如果你的图片是从网络上获取的,那么你一定不要用这种方法显示一张图片,因为这是最垃圾的办法,你的程序会经常被卡死。

建议您可以使用WebView来显示HTML内容。

赞助本站

人工智能实验室

相关热词: Android TextView HTML 图片

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港