减小 ImageSpan 的高度和宽度

Reduce ImageSpan height and width

我将 Image Drawable 设置为 TextView 的 SpannableString,但图像比文本大,看起来很奇怪。我需要减小 imagespan 的大小,使其与文本的高度相同:

这是我试过的方法:

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

SpannableStringBuilder builder = new SpannableStringBuilder(holder.temptext.getText());
builder.setSpan(new ImageSpan(drawable), selectionCursor - ":)".length(), selectionCursor, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.temptext.setText(builder);
holder.temptext.setSelection(selectionCursor);

holder.caption.setText(builder);

您需要测量您 TextView 的身高并使用它代替固有界限:

int lineHeight = holder.temptext.getLineHeight();

drawable.setBounds(0, 0, lineHeight, lineHeight);