有什么方法可以在不破坏文本的情况下在TextView中插入ImageSpan? [英] Is there any way to insert an ImageSpan in a TextView without disrupting the text?

查看:237
本文介绍了有什么方法可以在不破坏文本的情况下在TextView中插入ImageSpan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎要在Android的Spannable中添加ImageSpan,我实际上必须用Image替换一些文本.例如:

It seems that in order to add an ImageSpan to a Spannable in Android, I have to actually replace some text with the Image. For example:

Spannable span = new SpannableString("Foo imageplace Bar!");
Drawable android = context.getResources().getDrawable(R.drawable.android);
android.setBounds(0, 0, 32,32);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, 4, 14, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

这将用图像替换"imageplace".因为我要处理复杂的多跨度文本和重复,所以在我想要android图标的每个位置插入无意义的文本有点头疼.但是,如果跨度的开始和结束相同,则不会包含该图像.有什么办法解决吗?

This will replace "imageplace" with the image. Because I'm dealing with complex multi-span text and reiteration, it's a bit of a headache to insert meaningless text at each place I want the android icon. But it looks like if start and end of span are the same, the image won't be included. Is there any way around this?

推荐答案

也许您可以添加一个额外的空间,以用ImageSpan代替.例如

Maybe you can add an additional space to be replaced by the ImageSpan. For example,

Spannable span = new SpannableString("Foo  imageplace Bar!");
Drawable android = context.getResources().getDrawable(R.drawable.android);
android.setBounds(0, 0, 32,32);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

您会发现图像替换了额外的空间,而不会破坏文本.

And you will find the image replace the additional space without disrupting the text.

这篇关于有什么方法可以在不破坏文本的情况下在TextView中插入ImageSpan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆