如何在文本中插入drawables [英] How to insert drawables in text

查看:127
本文介绍了如何在文本中插入drawables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将小图片(例如箭头图标)插入 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屋!

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