Android TextView新行无效

时间:2017-06-15 04:29:02

标签: java android

我找到答案我将在2天内接受

我一直在尝试创建一个可以为用户显示多条消息的活动,以便在文本视图中适应我需要创建三行的所有内容。我的在线搜索没有给我任何解决方案,这是我试过的

爪哇

"Elapsed time: 0.468492 msecs"
"Elapsed time: 0.0796 msecs"

XML

"\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"));

杂项

  • 将字符串值直接硬编码到setText()
  • 以上所有的各种组合
  • 删除android:clickable =“false”
  • 删除android:cursorVisible =“false”
  • 删除android:focusable =“false”
  • 删除android:focusableInTouchMode =“false”

代码段:

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

TextView的当前和更新的XML:

// 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);

如果这有帮助,我使用的是Windows 7,Android Studio 2.3.2,Java 1.8,专门设计适用于SM-T580的应用程序(Samsung Tab A 10.1“),TextView的父级是组件树的基本ConstraintLayout

9 个答案:

答案 0 :(得分: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标签中添加内容

答案 1 :(得分:1)

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

答案 2 :(得分:1)

发现我必须采取android的问题:inputType =&#34; textPersonName&#34;用于TextView的XML。当我最初删除android时,我错误地离开了Html.fromHtml(...)方法调用:inputType =&#34; textPersonName&#34;。在调用message.setText之后(&#34;这个\ n是\ n A测试&#34;);没有android:inputType =&#34; textPersonName.Everything按预期工作。

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

换行符&#34; \ n&#34;如果输入类型是&#34; textPersonName&#34;

,则无效

Andriod的换行符是&#34; \ n&#34;

答案 3 :(得分:1)

由于这是一个TextView,因此无需为 android:inputType 进行设置。这些是我成功实施新行的步骤:

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

set maxLine inside xml file

  1. 如果要操作不是恒定的或未在string.xml中声明的字符串,请将System.getProperty(“ line.separator”)放在两个字符串之间。就我而言,我放

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

  2. 如果您有一个常量字符串,或者已将其设置为string.xml文件,则只需输入“ \ n”即可。请参阅下面的示例:

    string.xml

我正在使用recyclerView进行显示,所以这是输出:

Output

答案 4 :(得分:0)

试试这个。它对我有用

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);

答案 5 :(得分:0)

试试这个\ n对应于ASCII char 0xA,这是&#39; LF&#39;或换行

        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>

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

答案 6 :(得分:0)

将此代码添加到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>

添加此活动类

TextView tv = (TextView)findViewById(R.id.foo);
tv.setText(Html.fromHtml(getString(R.string.myhtml)));

答案 7 :(得分:0)

这对我有用:

messageTextView.setText(Html.fromHtml("This<br/>Is<br/>A Test"));

说明:TextView内容是HTML内容。 <br/>是换行符的HTML标记。

答案 8 :(得分:-2)

尝试了很多,最后对我有用

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

相关问题