Espaciado de línea individual para cada línea

¿Es posible definir TextView línea individuales para cada línea de texto de un TextView ?

Ejemplo:

 TextView tv = new TextView(context); tv.setText("line1\nline2\nline3"); 

El método setLineSpacing(float add, float mult) define los espaciados de línea para todas las líneas de texto de TextView . Me gustaría definir otro espaciado de línea entre line1 y line2 y un espaciado de línea diferente entre line2 y line3.

Alguna idea de como hacer esto ?

¿Un spannable proporciona una solución?

Sí, puede hacerlo utilizando la interfaz LineHeightSpan . Aquí hay un código de muestra rápido y sucio sobre cómo hacer esto:

 public class MyActivity extends Activity { private static class MySpan implements LineHeightSpan { private final int height; MySpan(int height) { this.height = height; } @Override public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) { fm.bottom += height; fm.descent += height; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TextView tv = new TextView(this); setContentView(tv); tv.setText("Lines:\n", BufferType.EDITABLE); appendLine(tv.getEditableText(), "Line 1 = 40\n", 40); appendLine(tv.getEditableText(), "Line 2 = 30\n", 30); appendLine(tv.getEditableText(), "Line 3 = 20\n", 20); appendLine(tv.getEditableText(), "Line 4 = 10\n", 10); } private void appendLine(Editable text, String string, int height) { final int start = text.length(); text.append(string); final int end = text.length(); text.setSpan(new MySpan(height), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } 

Usted puede hacer este tipo de estilos específicos a la textview usando Html. Pruebe este ejemplo,

 tv.setText(Html.fromHtml("<h2>Text1</h2><br><p>Text2</p>")); 

Diferentes tipos de etiquetas permitidas aquí son,

 <a href="..."> <b> <big> <blockquote> <br> <cite> <dfn> <div align="..."> <em> <font size="..." color="..." face="..."> <h1> <h2> <h3> <h4> <h5> <h6> <i> <img src="..."> <p> <small> <strike> <strong> <sub> <sup> <tt> <u> 

No es posible. Esta técnica se utiliza en c, c ++. Lo que se usa dentro de setText (), muestra todo el texto que se escribe.

Prueba esto:

 android:lineSpacingMultiplier = "3" 

El espaciado de línea también puede estar en formato decimal.

  • TextView setText problema
  • Enfocar el siguiente texto automáticamente
  • En un Android TextView, ¿es posible insertar párrafos?
  • Multilínea TextView
  • Android cómo hacer TextView texto en negrita cuando se presiona o centrado
  • Palabra especial de Higlight en la vista de texto
  • Android: Crear TextView que parpadea cuando se hace clic
  • Android - Configuración de TextView a negrita no funciona
  • Selector de texto no funciona
  • Custom TextView en android con diferentes palabras de color
  • Cómo ajustar la altura de línea en TextView con varios tamaños de tipo?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.