Android TextView换行无效。

5
我发现了答案,我会在两天内接受它。
我一直在尝试创建一个可以显示多个用户消息的活动,为了让所有内容都能适应文本视图,我需要创建三行。我在网上搜索没有得到任何解决方案,以下是我尝试过的:
Java
"\n"
"\r\n"
newLineChar = System.getProperty("line.separator")
messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));
messageTextView.setText(Html.fromHtml("This<br> Is<br> A Test"));

xml

android:lines="3"
android:maxLines="3"

杂项。

  • 将字符串值直接硬编码到setText()中
  • 所有上述组合
  • 移除android:clickable="false"
  • 移除android:cursorVisible="false"
  • 移除android:focusable="false"
  • 移除android:focusableInTouchMode="false"

代码片段:

// Message passed to next activity via putExtra()
message = "This\n Is\n A Test";
// Next Activity
TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
String message = getIntent().getStringExtra("message");
messageTextView.setText(message);

TextView的当前和更新的XML:

<TextView
    android:id="@+id/messageToUser"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:background="#90FFFFFF"
    android:ems="10"
    android:fontFamily="serif"
    android:text=""
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="30sp"
    android:textStyle="bold"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginStart="165dp"
    android:layout_marginEnd="165dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="209dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

如果有帮助的话,我使用的是Windows 7操作系统,Android Studio版本为2.3.2,Java版本是1.8。我正在设计一款专门用于SM-T580 (三星Tab A 10.1英寸平板电脑)的应用程序,TextView的父级是组件树中基础的ConstraintLayout。


在设置文本后,可以像这样操作:messageTextView.setText(messageTextView.replace("\n", "\n")); - Tufan
@pianoisland 除了你的三星Tab A 10.1之外,它能在其他设备/版本上运行吗? - Nitin Patel
@NitinPatel 不,这是唯一可以运行此应用程序的设备,感谢您阅读完整个问题。 - pianoisland
@Avi 在问题中添加了 XML - pianoisland
1
请勿使用 emsinputType - Nitin Patel
显示剩余8条评论
9个回答

5

尝试这个 \n 对应于ASCII字符0xA,即'LF'或换行符。

        tv.setText("First line " + System.getProperty("line.separator") + "Line 2"+ System.getProperty("line.separator") + "Line 3");


String String1 = "value 1";
String String2 = "value 2";
TextView.setText(String1 + "\n" + String2);

或者尝试这个。
string = string.replace("\\\n", System.getProperty("line.separator"));

如果需要高级字符串操作,请尝试以下方法

<string name="value"> This\nis a sample string data</string>

or

<string name="value> This<br>is a sample<br> String data</string>

1
"\n对我不起作用,System.getProperty("line.separator")也不行。字符串必须以编程方式传递,因此我无法将字符串硬编码到XML中。" - pianoisland
@pianoisland 试试这个 tv.setText("第一行 " + System.getProperty("line.separator") + "第二行"+ System.getProperty("line.separator") + "第三行"); - AskNilesh

4

这是一个TextView,因此不需要设置android:inputType。下面是我成功实现新行的步骤:

  1. 确保android:maxLine未设置为1。如果设置为1,则不会实现新行。[请参阅屏幕截图]

在xml文件中设置maxLine

  1. 如果您想操作不是常量或未在string.xml中声明的字符串,请在两个字符串之间放置System.getProperty("line.separator")。在我的情况下,我放置了

    phoneNumber = memberProfile.getPhoneNumber() + System.getProperty("line.separator") + "09162343636";

  2. 如果您有常量字符串或已将其设置为string.xml文件,则只需输入“\n”即可完美运作。以下是示例:

    string.xml

我正在使用recyclerView进行显示,因此以下是输出结果:

输出结果


2

\r\n 对我有用

messageTextView.setText("First line\r\nNext line");

或者,您也可以使用字符串变量。

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

所以,将内容包装在CDATA中是必要的,并在其中添加断点作为HTML标签。

你能想到任何原因导致它不能为我工作吗?这可能与我的环境有关吗? - pianoisland
\r 不是必需的。Android基于Linux,\n 表示换行。 - Gabe Sechan
你在使用哪个平台来运行Android Studio?是Windows还是Linux? - Jay Patel
Windows,这是我的问题中提到的。 - pianoisland
1
@JayPatel 首先,它是\r和\n,不是斜杠。其次,Android是一个Linux操作系统。这才是重点 - 你运行的平台,而不是你编译的平台。这意味着你只需要Linux的换行符,\n。 - Gabe Sechan

2
这对我有效:
messageTextView.setText(Html.fromHtml("This<br/>Is<br/>A Test"));

解释:TextView的内容是HTML内容。<br/>是一个表示换行的HTML标签。


1
messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));

1
尝试这个。它对我有效。
message = "This"+System.getProperty("line.separator") 
+ "Is" + System.getProperty("line.separator") + "A Test";
// Next Activity
TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
String message = getIntent().getStringExtra("message");
messageTextView.setText(message);

这与为System.getProperty("line.separator")初始化变量有何不同? - pianoisland
System.getProperty("line.separator") 将返回与操作系统相关的换行符。 - squalle0nhart
这对我很有帮助。我从后端获取了文本字符串,唯一使换行符起作用的方法是使用System.getProperty("line.separator")进行替换。 - Luigi_Papardelle

1
将此代码添加到res/strings
<string name="myhtml">
<![CDATA[
<p>This is a <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>

将此代码添加到您的Activity类中。
TextView tv = (TextView)findViewById(R.id.foo);
tv.setText(Html.fromHtml(getString(R.string.myhtml)));

1

我发现了问题所在,需要从TextView的XML中删除android:inputType="textPersonName"。当我最初删除android:inputType="textPersonName"后,错误地保留了Html.fromHtml(...)方法调用。在没有android:inputType="textPersonName"参数的情况下调用message.setText("This\n Is\n A Test");之后,一切都按预期工作。

为了最终的清晰度....

如果输入类型是"textPersonName",换行符"\n"将不起作用。

Android中的换行符为"\n"。


谢谢你,这解决了我的问题! - cete3

-2

尝试了很多,最终这个方法对我有效

messageTextView.setText(string.replace("\n", ""+System.getProperty("line.separator")));


1
欢迎来到Stack Overflow。这个问题已经很老了,已经有一个被接受的答案以及许多其他(类似的)答案。在添加另一个答案之前,请问一下自己,那个晚到的答案有什么价值呢?它是否添加了新的内容?是因为之前的答案已经过时或者不再适用吗?如果是这样,那么你的答案应该更好地解释这一点。回答旧的(已经回答过的)问题时要小心。 - Ivo Mori

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