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!
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.
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"
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!
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"));
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"));
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
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
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