2011-10-27 16 views
7

Quizás es una pregunta estúpida, pero no puedo encontrar una forma de insertar una imagen pequeña al final de la segunda línea de TextView. La imagen siempre aparece a la derecha de la vista de texto completa, no al final de la línea.¿Cómo insertar ImageView al final de TextView multilínea?

Quiero insertar una imagen como esta:

TextTextTextTextTextTextTextTextTextText 
TextTextTextText. <ImageView> 

Lo que quiero decir es:

TextTextTextTextTextTextTextTextTextText <ImageView> 
TextTextTextText. 

espero que no haya manera de hacerlo.

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

código postal ........ –

+0

Dé su código xml, más fácil de ver el problema. – Carnal

+0

Tienen un código que. – Sver

Respuesta

12

crear un ImageSpan y agréguelo a su vista de texto. De esta manera, usted puede poner su imagen en la que desea en el texto

Ejemplo -

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

Eso es nuevo, ¿cómo lo usas? ¿Puedes mostrar algún código de uso? – bluefalcon

+0

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

+0

¡Gracias! eso es algo que aprendí hoy ... – bluefalcon

0

Podría ser no es la mejor solución, pero se puede tratar de usar Html.fromHtml para insertar la imagen en su línea.

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

Una mejor manera de completar es como la siguiente ...

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); 
Cuestiones relacionadas