2011-10-27 12 views
7

Forse è una domanda stupida, ma non riesco a trovare un modo per inserire una piccola immagine alla fine della seconda riga di TextView. L'immagine viene sempre visualizzata a destra dell'intera vista testo, non alla fine della riga.Come inserire ImageView alla fine di TextView multilinea?

voglio inserire un'immagine come questa:

TextTextTextTextTextTextTextTextTextText 
TextTextTextText. <ImageView> 

Quello che sto ottenendo è:

TextTextTextTextTextTextTextTextTextText <ImageView> 
TextTextTextText. 

spero che ci sia modo per farlo.

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

codice postale ........ –

+0

Lascia il tuo codice xml, più facile vedere il problema. – Carnal

+0

Avere un po 'di codice. – Sver

risposta

12

creare un ImageSpan e aggiungerlo al tuo TextView. In questo modo, si può mettere la vostra immagine in cui si desidera nel testo

Esempio -

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

Questo è nuovo, come lo usi? Puoi mostrare qualche codice d'uso? – bluefalcon

+0

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

+0

Grazie! questa è una cosa che ho imparato oggi .. – bluefalcon

0

Potrebbe non essere la soluzione migliore, ma si può provare a utilizzare Html.fromHtml di inserire un'immagine nella vostra linea.

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

Un modo migliore per completare è come la seguente ...

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); 
Problemi correlati