如何在 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
How do I add a newline to a TextView in Android?
提问by Pentium10
When I define a TextView
in xml
, how do I add a new line to it? \n
seems not to work.
当我定义TextView
in 时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"
\n
should 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 \n
in 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"));
Using the replace method you need to filter escapedlinefeeds (e.g.
'\\\n'
)Only then each instance of line feed
'\n'
escape chars gets rendered into the actual linefeed
使用您需要过滤转义换行符的替换方法(例如
'\\\n'
)只有这样,换行符
'\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 \n
is in "\n"
for it to work.
确保您\n
可以"\n"
使用它。
回答by ATOzTOA
My 2 cents, using Android TV.
我的 2 美分,使用 Android TV。
Add \n
in XML strings, while reading:
添加\n
XML 字符串,同时阅读:
public void setSubtitle(String subtitle) {
this.subtitle = subtitle.replace("\n", System.getProperty("line.separator"));
}