¿Cómo agrego una nueva línea a un TextView en Android?


Cuando defino un TextView en xml, ¿cómo le agrego una nueva línea? \n parece no funcionar.

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: \n-Line2\n-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />
Author: Onik, 2010-05-15

26 answers

No confíes en el editor visual. Tu código funciona en la uem.

 260
Author: Macarse,
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
2010-05-16 04:51:25

Intenta:

android:lines="2"

\n debería funcionar.

 62
Author: Christian,
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
2015-03-24 13:58:56

Intenta System.getProperty("line.separator");

 42
Author: kakopappa,
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-04-30 05:35:31

Creo que esto tiene algo que ver con su llamada HTM.fromHtml(subTitle): un "\n" no significa bupkis a HTML. Intente <br/> en lugar de "\n".

 32
Author: resnbl,
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-03-18 06:53:17

Primero, pon esto en tu textview:

android:maxLines="10"

Luego use \n en el texto de su textview.

MaxLines hace que TextView tenga como máximo esta cantidad de líneas de altura. Puede elegir otro número:)

 19
Author: aF.,
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-09-30 14:17:58

Probé todo lo anterior, hice una investigación propia que resultó en la siguiente solución para renderizar caracteres de escape de alimentación de línea:

string = string.replace("\\\n", System.getProperty("line.separator"));
  1. Usando el método replace necesitas filtrar escaped linefeeds (e. g.'\\\n')

  2. Solo entonces cada instancia de caracteres de escape de alimentación de línea '\n' se renderiza en la alimentación de línea real

Para este ejemplo utilicé una base de datos NoSQL de Google Apps (ScriptDB) con formato JSON datos.

Salud: D

 14
Author: Robert,
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-04-29 20:28:40
<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: \r\n-Line2\r\n-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

Creo que esto funcionará.

 10
Author: user3578286,
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
2016-03-03 14:00:14

Esto resolvió mi problema.

stringVar.replaceAll("\\\\n", "\\\n");
 7
Author: viki,
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-11-18 10:04:18

Acabo de resolver el mismo problema, poner debajo de los atributos en xml

android:lines="2" android:maxLines="4" android:singleLine="false"

Trabajo.Html.fromHtml("text1 <br> text2").toString() también funciona.

 7
Author: Ben Wong,
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
2016-03-03 13:47:40

Asegúrese de que su \n está en "\n" para que funcione.

 7
Author: Justin,
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-08-23 07:53:52

Mis 2 centavos, usando Android TV.

Agregue \n en cadenas XML, mientras lee:

public void setSubtitle(String subtitle) {
    this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
}
 6
Author: ATOzTOA,
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
2015-10-12 20:51:55

System.getProperty("line.separator"); esto funciona para mí.

 6
Author: João Victor,
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
2016-03-03 14:00:27

Una forma de hacer esto es usando Html etiquetas::

txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));
 5
Author: garry,
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-08-23 07:55:14

Necesidad de mantener

1.android:maxLines="no of lines"

2.Y use \n para obtener las siguientes líneas

 4
Author: Danny,
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-02-25 11:33:31

Si depura, verá que la cadena es en realidad "\ \r\ \n" o "\ \n", es decir, se escapó. Así que si masajeas esa cuerda, para deshacerte del extra \, tendrás tu solución. Esto es cierto especialmente si está leyendo desde una base de datos.

 4
Author: Tilottama Gaat,
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-08-23 07:55:36

También puedes añadir &lt;br&gt; en lugar de \n.

Y luego puedes agregar texto a TexView:

articleTextView.setText(Html.fromHtml(textForTextView));
 4
Author: mobiledev Alex,
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-08-23 07:55:49

Necesitas poner \n en el archivo string.xml

<string name="strtextparkcar">press Park my Car to store location \n</string>
 3
Author: simmash,
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-08-23 07:56:02

Para mí la solución fue agregar la siguiente línea al diseño:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >

Y \n se muestran como una nueva línea en el editor visual. Espero que ayude!

 2
Author: Rob,
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-05-20 22:52:18

Necesita poner el "\n" en el archivo strings.xml no dentro del diseño de página.

 2
Author: epicness42,
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-08-23 07:56:16

La respuesta de RuDrA05 es buena, cuando edito el XML en eclipse no funciona, pero cuando edito el XML con notepad++ SÍ funciona.

Lo mismo sucede si leo un archivo txt guardado con eclipse o notepad++

Tal vez esté relacionado con la codificación.

 1
Author: Sebastian Corradi,
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-06-18 02:37:24

Esto funciona bien. Compruébalo para tu aplicación.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1\nText 2\nText 3"/>
 1
Author: Palani kumar,
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
2016-03-18 07:33:50

Nota al margen: Texto en mayúscula usando

android:inputType="textCapCharacters"

O similar parece detener el \n de trabajar.

 1
Author: Voy,
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-02-14 12:17:05

Asegúrese de que está utilizando your_package.R y no android.R

 1
Author: Dror,
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-08-23 07:56:30

Para la nueva línea en TextView simplemente agregue \n en el medio de su texto funciona..

 0
Author: Nikhil Borad,
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
2015-08-18 06:59:37

Si el TextView está en cualquier Layout (LinearLayout por ejemplo), intente cambiar el atributo orientation a vertical como android:orientation="vertical" para el diseño o cambie el atributo TextView android:singleLine="true" para crear una nueva línea después de él.

 0
Author: Pavani,
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
2016-08-23 13:21:39
 android:text="Previous Line &#10; Next Line" 

Esto funcionará.

 0
Author: Viral Parmar,
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-07-23 16:54:24