2011-10-27 52 views
7

也許這是一個愚蠢的問題,但我無法找到一種方法來在TextView的第二行結束時插入小圖像。圖像總是顯示在整個文本視圖的右側,而不是行結束。如何在多行TextView的末尾插入ImageView?

我想插入圖片是這樣的:

TextTextTextTextTextTextTextTextTextText 
TextTextTextText. <ImageView> 

我所得到的是:

TextTextTextTextTextTextTextTextTextText <ImageView> 
TextTextTextText. 

我希望有辦法做到這一點。

源:

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/tv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#FFFFFF" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/tv" 
      android:src="@drawable/icon" /> 
    </RelativeLayout> 
+0

郵政編碼........ –

+0

給你的xml代碼,更容易看到問題。 – Carnal

+0

有一些代碼比。 – Sver

回答

12

創建ImageSpan並將其添加到您的textview。這樣一來,就可以把在那裏你在文本

實例想你的形象 -

ImageSpan imagespan = new ImageSpan(appContext, ressource); 
text.setSpan(imagespan, i, i+ strLength, 0); //text is an object of TextView 
+0

這是新的,你如何使用它?你能顯示一些使用代碼嗎? – bluefalcon

+0

ImageSpan imagespan = new ImageSpan(appContext,ressource); text.setSpan(imagespan,i,i + strLength,0); –

+0

謝謝!那是我今天學到的一件事.. – bluefalcon

0

它可能不是最好的解決辦法,但你可以嘗試使用Html.fromHtml插入圖片到你的線。

ImageGetter getter = new ImageGetter(){     
    public Drawable getDrawable(String source){ 
     Drawable d = null; 

     context.getResources().getDrawable(Integer.parseInt(source)); 

     if (d != null) 
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 

     return d; 
    } 
}; 
String imgString = <source string> + " <img src=\"R.drawable.icon\"/>"; 
((TextView)findViewById(R.id.tv)).setText(
    Html.fromHtml(imgString, getter, null)); 
4

One以完成它是作爲下面的最佳途徑......

ImageGetter getter = new ImageGetter(){     
    public Drawable getDrawable(String source){ // source is the resource name 
     Drawable d = null; 
     Integer id = new Integer(0); 
     id = getApplicationContext().getResources().getIdentifier(source, "drawable", "com.sampleproject"); 

     d = getApplicationContext().getResources().getDrawable(id); 

     if (d != null) 
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 

     return d; 
    } 
}; 

String imgString = this.getResources().getString(R.string.text_string) + " <img src=\"img_drawable_image\"/>"; 
((TextView)findViewById(R.id.textview_id)).setText(
Html.fromHtml(imgString, getter, null)); 
0
SpannableString ss = new SpannableString(title); 
Drawable d = getResources().getDrawable(R.drawable.gallery_list); 
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
newsTextView.setText(ss); 
0
TextView textView =new TextView(this); 
SpannableStringBuilder ssb = new SpannableStringBuilder("Here's a smiley how are you "); 
Bitmap smiley = BitmapFactory.decodeResource(getResources(), R.drawable.movie_add); 
ssb.setSpan(new ImageSpan(smiley), ssb.length()-1, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
textView.setText(ssb, BufferType.SPANNABLE);