Android TextView中有圖片有文字混合排列
Android TextView中有圖片有文字混合排列
1.使用html.fromHtml
2.建立ImageGetter
3.使用標籤
demo:
1.設定文字
((TextView) findViewById(R.id.tv_gradlist_calorie_desc)).setText(Html .fromHtml(descString(), getImageGetterInstance(), null));
2.擷取文字
/** * 字串 * * @return */ private String descString() { return "您消耗的總熱量約等於4杯" + "" + ""; }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; }效果