TextView tv_text = (TextView) findViewById(R.id.tv);
String html="<font>草莓</font><img src=‘ic_launcher’><font>草莓</font>";
CharSequence text=Html.fromHtml(html, new ImageGetter() {
public Drawable getDrawable(String source) {
//根据图片资源ID获取图片
Log.d("source", source);
if(source.equals("‘ic_launcher’")){
Drawable draw=getResources().getDrawable(R.drawable.ic_launcher);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
return draw;
}
return null;
}
}, null);
tv_text.setText(text); TextView添加图片实现图文混排
最新推荐文章于 2025-07-14 13:27:46 发布
本文展示了一个如何在Android应用中使用HTML格式字符串并结合自定义Drawable显示图文混排的例子。通过覆写ImageGetter接口,可以实现加载自定义图片资源。
659

被折叠的 条评论
为什么被折叠?



