如何在文本中插入drawables [英] How to insert drawables in text
问题描述
我想将小图片(例如箭头图标)插入 TextView
内容的某些位置。
I want to insert small pictures, like arrow icons for example, into certain positions of contents of a TextView
.
下面的照片描述了我想要的内容:
The photo below depicts exactly what I want:
显然,最天真的解决方案是使用多个 TextView
在小 ImageView
对象的两侧。但这种方法不具备可扩展性。
Obviously, the most naive solution is to use multiple TextView
on either side of the small ImageView
objects. But this approach is not scalable.
我很想知道是否有人用一些简单而聪明的技巧克服了这个问题。
(也许在HTML或外部库的帮助下)
I am curious to learn if someone has overcome this problem with some simple yet smart trick. (Maybe with help from HTML or an external library)
非常感谢任何有效的解决方案。
Any efficient solution is much appreciated.
推荐答案
您可以创建SpannableString并将任何对象添加到您的字符串
You can create SpannableString and add any object to your string
TextView textView = (TextView) findViewById(R.id.textView);
ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_launcher);
SpannableString spannableString = new SpannableString(textView.getText());
int start = 3;
int end = 4;
int flag = 0;
spannableString.setSpan(imageSpan, start, end, flag);
textView.setText(spannableString);
这篇关于如何在文本中插入drawables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!