使用 spannablestring 在文本左侧附加图像

Jos*_*369 3 android inline image textview xamarin

我想在我的文本视图中,在文本本身之前显示一个图像。我试过了,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
Run Code Online (Sandbox Code Playgroud)

但是图像在开头而不是结尾。

小智 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);
Run Code Online (Sandbox Code Playgroud)

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

sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

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