使用Html.fromhtml()在textview中显示图像; [英] Show image in textview using Html.fromhtml();

查看:519
本文介绍了使用Html.fromhtml()在textview中显示图像;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表适配器来显示我的社交网络的内容,我需要在文本里面放入img标签,我试过使用Html.fromhtml,它的格式是文本,但不是显示img,它显示灰色平方。



我该如何实现这一目标?我已经阅读了关于ImageGetter的内容,但我仍然没有清楚。



谢谢

解决您可以使用 Html在 TextView 中绘制图像 img标签 Html.ImageGetter 。但请确保您的图像可用于资源可绘制文件夹



下面是一个示例,图像将从

 字符串htmlText =Hai< img src = \ic_launcher \> Hello; 

textView.setText(Html.fromHtml(htmlText,new Html.ImageGetter(){
$ b @Override
public Drawable getDrawable(String source){
int resourceId = getResources()。getIdentifier(source,drawable,getPackageName());
Drawable drawable = getResources()。getDrawable(resourceId);
drawable.setBounds(0,0,drawable .getIntrinsicWidth(),drawable.getIntrinsicHeight());
返回drawable;
}
},null));


I have a list adapter to show content frm my social network, i need to put inside the text the img tags, i've tried using Html.fromhtml and it's formatting the text but instead of shows the img it shows a gray square.

How can i achieve that? I'v read about ImageGetter but i still don't have it very clear.

Thanks

解决方案

You can draw an Image in TextView using the Html img tag with Html.ImageGetter. But make sure your image is available in resource drawable folder

Here is a sample , The image will be loaded from the resource.

String htmlText = "Hai <img src=\"ic_launcher\"> Hello";

textView.setText(Html.fromHtml(htmlText, new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
   int resourceId = getResources().getIdentifier(source, "drawable",getPackageName());
   Drawable drawable = getResources().getDrawable(resourceId);
   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            return drawable;
        }
    }, null));

这篇关于使用Html.fromhtml()在textview中显示图像;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆