2010-11-30 24 views
6

Me gustaría saber si alguien podría ayudarme con esto.Ajustar el fondo al texto en TextView en android

Tengo un textView y quiero que el fondo se aplique solo al texto, de acuerdo con su tamaño, no a todo el textView.

No hay ningún problema cuando el texto es sólo una lenghth fila, pero no cuando se necesita más de una línea ...

Tal vez este dibujo ayuda a comprender todo esto ...

superior : lo que tengo inferior: lo que quiero

alt text

Gracias :-) USI

Respuesta

3

Try ng algo como esto:

TextView tv = (TextView) findViewById(R.id.myid); 
tv.setText(Html.fromHtml("<span style="background: red;">Text</span>")); 

Podría resolver el problema ...

EDIT:

he encontrado la solución aquí: Set color of TextView span in Android usando SpannableString

+0

Gracias Mikpa :-) Pero' no funciona, ya que dice en el documento "Vista de Texto no acepta en HTML como el formateo "pero me diste la idea de usar así que encontré esto, usando SpannableString: http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android –

0

El uso de etiqueta span HTML doesn' t trabajo para mi Prueba este código que se ejecuta:

String str = "This is highlighted text"; 
TextView textview = (TextView) findViewById (R.id.textview); 
SpannableStringBuilder style = new SpannableStringBuilder (str); 
style.setSpan (new BackgroundColorSpan (Color.RED), 0, str.lenght(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textview.setText(style); 
0

Esto funcionó para mí

TextView tv = (TextView) findViewById(R.id.tvMyText); tv.setText("Some Text"); tv.setBackgroundColor(Color.CYAN);

Cuestiones relacionadas