使用SpannableString在文本左侧添加图片

3

我希望在文本视图中展示一张图片,图片显示在文本前面。我尝试使用CompoundDrawable,但第二行的文字并没有在第一行的图片下方。

我需要像这样的效果:

var imageSpan = new ImageSpan(this, Resource.Drawable.Icon); //Find your drawable.
var spannableString = new SpannableString(textView.Text); //Set text SpannableString from TextView
spannableString.SetSpan(imageSpan, textView.Text.Length -1, textView.Text.Length, 0); //Add image at end of string

但是图片应该放在开头而不是结尾。

你在XML中尝试过android:setDrawableLeft吗? - Noor Nawaz
1个回答

5
Drawable image = context.getResources().getDrawable(imageResId[position]);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

我不知道为什么,但是0和1看起来很重要:

sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接