垂直对齐TextView中由HTML定义的图像 [英] Vertically align images defined by HTML in TextView

查看:148
本文介绍了垂直对齐TextView中由HTML定义的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView ,其中包含html中的文字和图片,即带有< img src = \的一些文字你好\/> image

要正确显示图像,请使用 Html.ImageGetter 请求的资源可绘制并根据 TextView 的高度设置其边界。

目前看起来图像与基线对齐 TextView ,而我希望它们垂直居中显示。是否可能以及如何?

I have a TextView containing text and image(s) in html, i.e. "some text with <img src=\"hello\"/> image".
To display images properly the Html.ImageGetter is used, which reads the requested drawable from resources and sets its bounds according on the TextView's height.
Currently it looks like the images are aligned to the baseline of the TextView, while I would like them to appear vertically centered. Is it possible and how?

推荐答案

此代码段将为您提供帮助。

This code snippet will help you.

private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
    BitmapDrawable d = getPicture(source);
    d.setBounds(new Rect(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight()+5));
    d.setGravity(Gravity.TOP);
    return d;
  } 
};

这篇关于垂直对齐TextView中由HTML定义的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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