2015-06-01 24 views
1

我想在文本視圖中顯示一個圖像,在文本本身的前面。我試過CompoundDrawable,但第二行的文本不在第一行的圖像下面。使用spannablestring追加文本左邊的圖像

我需要是這樣的:在結束的開始,而不是

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 

但圖像。

+0

您是否嘗試過的Android? –

回答

2
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看起來很重要:在XML setDrawableLeft:

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

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