我有一个TextView,它利用android:lineSpacingMultiplier属性来增加行之间的间距,除了在文本中添加ImageSpan之外,它可以很好地工作。
这会导致图像与行间空格的底部对齐,而不是文本的基线(正如我在创建它时指定的那样)。
我尝试使用android:lineSpacingExtra属性,取得了一些成功,图像的位置仍然比它应该的位置低,但没有那么多。有没有其他方法可以在不影响ImageSpan垂直对齐的情况下增加行间距
发布于 2014-09-02 23:53:36
在构造ImageSpan时,可以指定垂直对齐方式,可以是ImageSpan.ALIGN_BOTTOM或ImageSpan.ALIGN_BASELINE之一。我相信ImageSpan在默认情况下使用ALIGN_BOTTOM,所以尝试使用一个允许您指定ALIGN_BASELINE的构造函数。
发布于 2015-02-06 16:20:07
我也遇到过同样的问题,行距改变了基线,所以当你输入文本时,它会删除图像……您必须通过更改其绘图方法来实现您的自定义图像跨度:
public class CustomImageSpan extends ImageSpan{
public static final int ALIGN_TOP = 2;
public static final int ALIGN_CUSTOM = 3;
@Override
public void draw(Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, Paint paint) {
Drawable b = getCachedDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
} else if (mVerticalAlignment == ALIGN_TOP) {
transY += paint.getFontMetricsInt().ascent;
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
private Drawable getCachedDrawable() {
WeakReference<Drawable> wr = mDrawableRef;
Drawable d = null;
if (wr != null)
d = wr.get();
if (d == null) {
d = getDrawable();
mDrawableRef = new WeakReference<Drawable>(d);
}
return d;
}
private WeakReference<Drawable> mDrawableRef;
}https://stackoverflow.com/questions/21406548
复制相似问题