默认的ImageSpan只能实现和文字向上(ImageSpan.ALIGN_BASELINE)和向下(ImageSpan.ALIGN_BOTTOM)对齐,所以是无法实现ImageSpan和文字垂直方向在一条直线上的。
当然也是能实现的,自定义一一个ImageSpan就行,下面是代码,来自So。
public class CenteredImageSpan extends ImageSpan
{
private WeakReference 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;
fm.bottom = pfm.bottom;
}
return rect.right;
}
@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 drawableHeight = b.getIntrinsicHeight();
int fontAscent = paint.getFontMetricsInt().ascent;
int fontDescent = paint.getFontMetricsInt().descent;
int transY = bottom - b.getBounds().bottom + (drawableHeight - fontDescent + fontAscent) / 2;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
// Redefined locally because it is a private member from
private Drawable getCachedDrawable()
{
WeakReference wr = mDrawableRef;
Drawable d = null;
if (wr != null)
d = wr.get();
if (d == null)
{
d = getDrawable();
mDrawableRef = new WeakReference(d);
}
return d;
}
}
在做上面的需求的时候,一直不太明白SpannableString的一些使用,
SpannableString result1 = new SpannableString(title);
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
result1.setSpan(span, 2, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
相应的java代码。
SpannableString ss1 = new SpannableString("text4: Click here to dial the phone.");
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString ss2 = new SpannableString("text4: Click here to dial the phone.");
ss2.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
ss2.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SpannableString ss3 = new SpannableString("text4: Click here to dial the phone.");
ss3.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
ss3.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
SpannableString ss4 = new SpannableString("text4: Click here to dial the phone.");
ss4.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss4.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
((EditText) findViewById(R.id.text1)).setText(ss1);
// ((EditText)
// findViewById(R.id.text1)).setMovementMethod(LinkMovementMethod.getInstance());
((EditText) findViewById(R.id.text2)).setText(ss2);
// ((EditText)
// findViewById(R.id.text2)).setMovementMethod(LinkMovementMethod.getInstance());
((EditText) findViewById(R.id.text3)).setText(ss3);
// ((EditText)
// findViewById(R.id.text3)).setMovementMethod(LinkMovementMethod.getInstance());
((EditText) findViewById(R.id.text4)).setText(ss4);
// ((EditText)
// findViewById(R.id.text4)).setMovementMethod(LinkMovementMethod.getInstance());
它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。即代表的前后的文字是否也一样,哈哈。