Cómo centrar un texto de dos líneas en un TextView en Android?


Quiero que se vea así:

|    two    |
|   lines   |

Aquí está el diseño actual, no funciona en absoluto.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two\nlines"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true"/>
</RelativeLayout>

Alguna idea? ¡Gracias!

Author: i_am_jorf, 2011-01-25

7 answers

Si solo desea centrarlo (asumo que \n está trabajando para dividir las líneas), solo agregue android:gravity="center_horizontal" en lugar de layout_gravity.
Usando layout gravity mueve el TextView real, usando gravity afecta el contenido del TextView.

 194
Author: kcoppock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-14 12:19:16

Tuvo que agregar gravity y layout_gravity para alinear ambos TextView y su contenido

android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
 10
Author: kishor.j,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-16 03:20:02

Si su ancho es wrap_content, debe establecer gravity y layout_gravity como "center_horizontal"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="two\nlines"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"/>

Sin embargo, si su ancho es "match_parent, solo tendrá que establecer la gravedad como "center_horizontal"

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="two\nlines"
    android:gravity="center_horizontal"/>

Espero que ayude!

 3
Author: Shivam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-02 07:50:09

Puedes usar:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));
 2
Author: Dleelk4android,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-07 14:55:15

Creo que debes hacerlo desde Java:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/the_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true"/>
</RelativeLayout>

Entonces:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two<br/>lines"));
 1
Author: Cristian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-25 02:58:54

Utilizo:

android:gravity="center_horizontal"

Para centrar el texto de dos líneas

 0
Author: Dea Venditama,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-23 02:34:17

Añadir propiedad

android:lines="2"

Donde 2 es no de líneas, puede establecer este no como su requerimiento

 -3
Author: Siddhartha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-11 07:16:59