如何将TextView中的文本设置为两行?

10

有没有办法以编程方式将TextView中的文本设置为两行?

4个回答

21

输入\n以换行

This will be\ntwo lines

看起来会像什么

This will be
two lines

同时,请确保TextView的setSingleLine()方法未设置为true。


还要检查一下你的XML中是否有android:maxLines的限制。 - Zon

9
TextView tv = new TextView(this);
tv.setText("First Line \n this is Second Line");
setContentView(tv);

4
您可以使用 \n 在文本视图中插入换行符。

就像这样:

playing.setText( "STATION: \n" + stations.get( position ) );

1
请确保您的TextView在xml中有几行代码:
android:lines="2" android:singleLine="false"
默认情况下,android:singleLine为true。因此,您可以在XML或Java代码中设置它,例如:
txtNextLine.setSingleLine(false);
txtNotActivate.setText("first line\n"+"second line");

希望这能有所帮助。

1
singleLine is deprecated , use maxLine = "1" - Saurabh Bhandari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接