Android TextView中有图片有文字混合排列

Android TextView中有图片有文字混合排列

1.使用html.fromHtml

2.新建ImageGetter

3.使用<img src>标签

demo:

1.设置文字

     ((TextView) findViewById(R.id.tv_gradlist_calorie_desc)).setText(Html
.fromHtml(descString(), getImageGetterInstance(), null));

2.获取文字

    /**
* 字符串
*
* @return
*/
private String descString() {
return "您消耗的总热量约等于4杯" + "<img src='" + R.drawable.ic_gradlist_coke
+ "'/>" + "+5仅仅" + "<img src='" + R.drawable.ic_gradlist_ice
+ "'/>" + "+10个" + "<img src='"
+ R.drawable.ic_gradlist_hamburger + "'/>" + ""; }

3.imagegetter

    /**
* ImageGetter用于text图文混排
*
* @return
*/
public ImageGetter getImageGetterInstance() {
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int fontH = (int) (getResources().getDimension(
R.dimen.textSizeMedium) * 1.5);
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
int height = fontH;
int width = (int) ((float) d.getIntrinsicWidth() / (float) d
.getIntrinsicHeight()) * fontH;
if (width == 0) {
width = d.getIntrinsicWidth();
}
d.setBounds(0, 0, width, height);
return d;
}
};
return imgGetter;
}

效果

Android TextView中有图片有文字混合排列

上一篇:evak购物车--课程设计(201521123037邱晓娴)


下一篇:自定义组件---图片和文字实现ImageButton效果