默认的ImageSpan只能实现和文字向上(ImageSpan.ALIGN_BASELINE)和向下(ImageSpan.ALIGN_BOTTOM)对齐,所以是无法实现ImageSpan和文字垂直方向在一条直线上的。
当然也是能实现的,自定义一一个ImageSpan就行,下面是代码,来自So。
public class CenteredImageSpan extends ImageSpan
{
private WeakReference<Drawable> mDrawableRef;
public CenteredImageSpan(Drawable drawableRes)
{
super(drawableRes);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)
{
Drawable d = getCachedDrawable();
Rect rect = d.getBounds();
if (fm != null)
{
Paint.FontMetricsInt pfm = paint.getFontMetricsInt();
// keep it the same as paint's fm
fm.ascent = pfm.ascent;
fm.descent = pfm.descent;
fm.top = pfm.top;