2011-10-27 19 views
7

Có lẽ đó là một câu hỏi ngu ngốc, nhưng tôi không thể tìm thấy một cách để chèn hình ảnh nhỏ ở cuối dòng thứ hai của TextView. Hình ảnh luôn xuất hiện ở bên phải của toàn bộ chế độ xem văn bản, không xuất hiện ở đầu dòng.Làm cách nào để chèn ImageView vào cuối TextView đa dòng?

Tôi muốn chèn hình ảnh như thế này:

TextTextTextTextTextTextTextTextTextText 
TextTextTextText. <ImageView> 

Những gì tôi nhận được là:

TextTextTextTextTextTextTextTextTextText <ImageView> 
TextTextTextText. 

Tôi hy vọng có cách để làm điều đó.

Src:

<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

mã bưu điện ........ –

+0

Cung cấp mã xml của bạn, dễ dàng hơn để xem sự cố. – Carnal

+0

Có một số mã hơn. – Sver

Trả lời

12

tạo ImageSpan và thêm nó vào chế độ xem văn bản của bạn. Bằng cách này, bạn có thể đặt hình ảnh của bạn mà bạn muốn trong văn bản

Ví dụ -

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

Đó là mới, bạn sử dụng nó như thế nào? Bạn có thể hiển thị một số mã sử dụng không? – bluefalcon

+0

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

+0

Cảm ơn! thats một điều tôi đã học được ngày hôm nay .. – bluefalcon

0

Nó có thể là không phải là giải pháp tốt nhất, nhưng bạn có thể thử sử dụng Html.fromHtml để chèn hình ảnh vào dòng của bạn.

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

Một cách tốt nhất để hoàn thành nó là như sau ...

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); 
Các vấn đề liên quan