如何在Android中向TextView添加换行符?
当我在xml定义一个TextView时,如何添加一个新的行?  \n似乎不工作。 
 <TextView android:id="@+id/txtTitlevalue" android:text="Line1: \n-Line2\n-Line3" android:layout_width="54dip" android:layout_height="fill_parent" android:textSize="11px" /> 
	
不要相信可视化编辑器。 你的代码在emu中工作。
尝试:
 android:lines="2" 
  \n应该工作。 
 尝试System.getProperty("line.separator"); 
 我认为这与你的HTM.fromHtml(subTitle)调用有关:一个“\ n”并不意味着bupkis到HTML。 尝试<br/>而不是“\ n”。 
首先,把它放在你的textview中:
 android:maxLines="10" 
 然后在你的textview文本中使用\n 。 
maxLines使TextView最多只有这么多行。 你可以select另一个数字:)
尝试以上所有,做了一些我自己的研究导致以下解决scheme渲染换行符转义字符:
 string = string.replace("\\\n", System.getProperty("line.separator")); 
- 
使用replace方法,您需要过滤转义的换行符(例如 '\\\n')
- 
只有这样,每一个换行符 '\n'转义字符的实例才会被渲染成实际的换行符
在本例中,我使用了带有JSON格式化数据的Google Apps脚本noSQL数据库(ScriptDb)。
干杯:D
 <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" /> 
我认为这将工作。
确保您的\ n在“\ n”中才能正常工作。
这解决了我的问题。
 stringVar.replaceAll("\\\\n", "\\\n"); 
我只是解决了同样的问题,把下面的属性放在xml中
 android:lines="2" android:maxLines="4" android:singleLine="false" 
 工作。  Html.fromHtml("text1 <br> text2").toString()也可以。 
 System.getProperty("line.separator"); 这个工作对我来说。 
如果你debugging,你会看到string实际上是“\\ r \\ n”或“\\ n”,即它被转义。 所以,如果你按摩这个string,摆脱多余的\,你会有你的解决scheme。 这是真的,尤其是如果你正在从数据库中读取。
您也可以添加<br>而不是\ n。
然后你可以添加文本到TexView:
 articleTextView.setText(Html.fromHtml(textForTextView)); 
需要保持
  1. android:maxLines="no of lines" 
  2.使用\n获取下一行 
你需要把\ n放在文件string.xml中
 <string name="strtextparkcar">press Park my Car to store location \n</string> 
我的2美分,使用Android电视。
 阅读时在XMLstring中添加\n : 
 public void setSubtitle(String subtitle) { this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator")); } 
一个这样做的方法是使用Html标签::
 txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3")); 
您需要将stringxml文件中的“\ n”放在页面布局中。
确保你使用的是your_package.R而不是android.R
RuDrA05的答案是好的,当我在eclipse上编辑XML时不起作用,但是当我用记事本++编辑XML时,它确实起作用。
如果我读取保存在eclipse或notepad ++中的txt文件,情况也是如此
也许是有关的编码。
附注:使用大写字母
 android:inputType="textCapCharacters" 
 或类似似乎停止\n从工作。 
对于TextView中的新行,只需在文本中间添加\ n即可 。
这工作正常。 检查它的应用程序。
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text 1\nText 2\nText 3"/> 
 如果TextView是在任何布局(例如LinearLayout),尝试改变方向属性垂直为android:orientation="vertical"为布局或更改TextView属性android:singleLine="true"创build一个新的行后它。 
尝试这个,
 String string = "1.abc \n 2. gff \n 3. hjk"; string = string.replace("\\\n",System.getProperty("line.separator")); 
使用Notepad ++并按照以下步骤操作:
- 在记事本++中加载XML。
- 点击replace。
- 并将\ n全部replace为\ n。 (查找 – > \ nreplace – > \ n)
你很好走。