我想在TextView
的某些位置插入小图片,比如箭头图标。
下面的图片准确地反映了我的要求:
显然,最简单的解决方案是在小的ImageView
对象两侧使用多个TextView
。但这种方法不够可扩展。
我很想知道是否有人用一些简单而巧妙的技巧克服了这个问题。 (也许借助 HTML 或外部库)
任何高效的解决方案都将不胜感激。
我想在TextView
的某些位置插入小图片,比如箭头图标。
下面的图片准确地反映了我的要求:
显然,最简单的解决方案是在小的ImageView
对象两侧使用多个TextView
。但这种方法不够可扩展。
我很想知道是否有人用一些简单而巧妙的技巧克服了这个问题。 (也许借助 HTML 或外部库)
任何高效的解决方案都将不胜感激。
您可以创建SpannableString并向其中添加任何对象到您的字符串中。
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);
前不久有一个类似的问题,有人想出了一个很棒的解决方案。我只是稍微调整了一下,使图像大小始终与行高相同。因此,基本上你的图标将会随着文本大小进行缩放。
创建一个扩展TextView
的新Java类。
public class TextViewWithImages extends TextView {
public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewWithImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithImages(Context context) {
super(context);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spannable s = getTextWithImages(getContext(), text, this.getLineHeight());
super.setText(s, BufferType.SPANNABLE);
}
private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
private static boolean addImages(Context context, Spannable spannable, float height) {
Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
boolean hasChanges = false;
Matcher matcher = refImg.matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end()
) {
spannable.removeSpan(span);
} else {
set = false;
break;
}
}
String resName = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
int id = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());
Drawable mDrawable = context.getResources().getDrawable(id);
mDrawable.setBounds(0, 0, (int)height, (int)height);
if (set) {
hasChanges = true;
spannable.setSpan( new ImageSpan(mDrawable),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}
return hasChanges;
}
private static Spannable getTextWithImages(Context context, CharSequence text, float height) {
Spannable spannable = spannableFactory.newSpannable(text);
addImages(context, spannable, height);
return spannable;
}
}
现在在你的布局XML中,只需使用TextViewWithImages类即可。
<com.stacko.examples.TextViewWithImages
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/my_string_with_icons" />
正如您可以在TextViewWithImages
类的addImages(...)
函数中看到的那样,为了添加图像,字符串中使用了一种特殊的模式([img src=my_icon/]
)。
这里是一个例子:
<string name="my_string_with_icons">The [img src=ic_action_trash/] is used to delete an item while the [img src=ic_action_edit/] is to edit one.</string>
正如之前所说,它将会根据您的textSize
进行缩放:
TextViewWithImages
中,没有发生崩溃。您的设备运行哪个Android版本? - reVerseshrinkResources
时出现问题(其中资源将被丢弃,除非您有其他用途)-请查看 https://developer.android.com/studio/build/shrink-code.html -> 自定义保留哪些资源
- Florian MötzWebView
代替 TextView
。这也允许您显示来自资产文件夹的图像。