首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImageSpan未显示图像

ImageSpan未显示图像
EN

Stack Overflow用户
提问于 2020-10-28 16:25:57
回答 1查看 415关注 0票数 1

我想在自定义TextView的段落末尾放置一个小的动画图像,但当我使用以下代码时,它不会显示图像:

代码语言:javascript
复制
cmptDiag.setSpan(
  ImageSpan(context, R.drawable.diagpause_anim_gif, DynamicDrawableSpan.ALIGN_BASELINE),
  cmptDiag.length-2,
  cmptDiag.length-1,
  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

我怀疑'context‘会有更好的选择,但找不到有效的东西。getContext()不起作用,我不能把'this‘和'this@theCustomTextView’放在一起。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-29 15:15:52

请查看此示例。

代码语言:javascript
复制
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val text = ("Lorem Ipsum is simply dummy text of the printing   and" +
                " typesetting industry. Lorem Ipsum   has been the industry's" +
                " standard dummy text ever since the 1500s, when an unknown" +
                " printer took a galley   of type and scrambled it" +
                " to make a type specimen book.").toSpannable()

        // Get icon from drawable resource
        var icon:Bitmap = BitmapFactory.decodeResource(resources,R.drawable.cfsuman)

        // Scale bitmap using android kotlin core ktx function
        icon = icon.scale(100,100,false)

        // Image span from drawable icon
        text[49..50] = ImageSpan(this,R.drawable.ic_weekend)

        // Another image span from bitmap
        text[89..90] = ImageSpan(this,icon)

        textView.text = text
    }
}

此示例将以文本形式显示图像。

如果您想添加bound,请选中此。

代码语言:javascript
复制
Drawable image = ContextCompat.getDrawable(mContext, android.R.drawable.presence_offline);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
String myText = "myText";
int textLength = myText.length();
SpannableString sb = new SpannableString(myText + "   " + "This is another text");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, textLength, textLength + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

这个'setBound‘示例是由java编写的。但我想你可以换成kotlin

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64568970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档