如何在 Android 中向 TextView 添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2840608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:35:14  来源:igfitidea点击:

How do I add a newline to a TextView in Android?

androidnewlinetextview

提问by Pentium10

When I define a TextViewin xml, how do I add a new line to it? \nseems not to work.

当我定义TextViewin 时xml,如何向其添加新行?\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" />

回答by Macarse

Don't trust the Visual editor. Your code does work in the emu.

不要相信可视化编辑器。您的代码在 emu 中确实有效。

回答by Christian

Try:

尝试:

android:lines="2"

android:lines="2"

\nshould work.

\n应该管用。

回答by kakopappa

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

尝试 System.getProperty("line.separator");

回答by resnbl

I think this has something to do with your HTM.fromHtml(subTitle)call: a "\n" doesn't mean bupkis to HTML. Try <br/>instead of "\n".

我认为这与您的HTM.fromHtml(subTitle)电话有关:“\n”并不意味着对 HTML 的 bupkis。尝试<br/>代替“\n”。

回答by aF.

First, put this in your textview:

首先,把它放在你的文本视图中:

android:maxLines="10"

Then use \nin the text of your textview.

然后\n在你的 textview 的文本中使用。

maxLines makes the TextView be at most this many lines tall. You may choose another number :)

maxLines 使 TextView 最多有这么多行高。您可以选择其他号码:)

回答by Robert

Tried all the above, did some research of my own resulting in the following solution for rendering line feed escape chars:

尝试了以上所有方法,我自己做了一些研究,得出了以下用于渲染换行符转义字符的解决方案:

string = string.replace("\\n", System.getProperty("line.separator"));
  1. Using the replace method you need to filter escapedlinefeeds (e.g. '\\\n')

  2. Only then each instance of line feed '\n'escape chars gets rendered into the actual linefeed

  1. 使用您需要过滤转义换行符的替换方法(例如'\\\n'

  2. 只有这样,换行符'\n'转义字符的每个实例才会被渲染成实际的换行符

For this example I used a Google Apps Scripting noSQL database (ScriptDb) with JSON formated data.

在本示例中,我使用了带有 JSON 格式数据的 Google Apps Scripting noSQL 数据库 (ScriptDb)。

Cheers :D

干杯:D

回答by user3578286

<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" />

I think this will work.

我认为这会奏效。

回答by Ben Wong

I just solve the same problem, put below attributes in xml

我只是解决了同样的问题,将下面的属性放在 xml 中

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

work.Html.fromHtml("text1 <br> text2").toString()also work.

工作。Html.fromHtml("text1 <br> text2").toString()也工作。

回答by Justin

Make sure your \nis in "\n"for it to work.

确保您\n可以"\n"使用它。

回答by ATOzTOA

My 2 cents, using Android TV.

我的 2 美分,使用 Android TV。

Add \nin XML strings, while reading:

添加\nXML 字符串,同时阅读:

public void setSubtitle(String subtitle) {
    this.subtitle = subtitle.replace("\n", System.getProperty("line.separator"));
}