android - 减小 ImageSpan 的高度和宽度

标签 android android-layout textview spannablestring imagespan

我将 Image Drawable 设置为 SpannableString 到 TextView,但图像比文本大,看起来很奇怪。我需要减小 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);

关于android - 减小 ImageSpan 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298853/

相关文章:

user-interface - 如何在android中添加文本稀松布

java - 向 TextView 添加一个字母

java - Intellij IDEA 崩溃,现在抛出错误

java - Android 效率 - View

android - 如何在drawable中引用颜色属性?

android 使 LinearLayout 及其子元素透明

android - 如何以编程方式在 Android TextView 中设置 maxLength?

安卓视频镜像效果

java - 使用 ScrimInsetFrameLayout 出错

php - 无法重定向到 MainActivity 屏幕