Android textview 不支持换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11380633/
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
Android textview not supporting line break
提问by wyoskibum
I'm creating a custom view programmatically that is displaying text that is parsed from an XML file. The text is long and contains the "/n" character for force line breaks. For some reason, the text view is displaying the /n and there isn't any line breaks. Here is my code:
我正在以编程方式创建一个自定义视图,该视图显示从 XML 文件解析的文本。文本很长并且包含强制换行符的“/n”字符。出于某种原因,文本视图显示 /n 并且没有任何换行符。这是我的代码:
// get the first section body
Object body1 = tempDict.get("FIRE");
String fireText = body1.toString();
// create the section body
TextView fireBody = new TextView(getActivity());
fireBody.setTextColor(getResources().getColor(R.color.black));
fireBody.setText(fireText);
fireBody.setTextSize(14);
fireBody.setSingleLine(false);
fireBody.setMaxLines(20);
fireBody.setBackgroundColor(getResources().getColor(R.color.white));
// set the margins and add to view
layoutParams.setMargins(10, 0, 10, 0);
childView.addView(fireBody,layoutParams);
The text from the XML file is like this:
XML 文件中的文本如下所示:
Now is the time /n for all good men to /n come to the aid of their /n party
It should display as such;
它应该这样显示;
Now is the time
for all good men to
come to the aid of their
party
Is there are setting that I'm missing?
是否有我遗漏的设置?
UPDATE
更新
\r\n works if I hard code it into my view. ie:
\r\n 如果我将它硬编码到我的视图中就可以工作。IE:
String fireText = "Now is the time \r\n for all good men \r\n to come to the aid";
Actually \n also works if i hard code it:
实际上 \n 如果我硬编码它也可以工作:
String fireText = "Line one\nLine two\nLine three";
FYI
供参考
System.getProperty("line.separator");
this returns a string of "/n" so there is no need to convert to "/r/n".
这将返回一个“/n”字符串,因此无需转换为“/r/n”。
Unfortunately, my data originates in a XML file that is parsed and stored in a hashmap. I tried the following:
不幸的是,我的数据源自一个 XML 文件,该文件被解析并存储在哈希图中。我尝试了以下方法:
String fireText = body1.toString().replaceAll("\n", "\r\n");
The \n is not getting replaced with \r\n. Could it be because I'm converting from an object to String?
\n 没有被替换为 \r\n。可能是因为我正在从对象转换为字符串?
回答by Rob
I've been having the exactsame problem under the exactsame circumstances. The solution is fairly straight forward.
在完全相同的情况下,我一直遇到完全相同的问题。解决方案相当简单。
When you think about it, since the textview widget is displaying the text with the literal "\n" values, then the string that it is being given must be storing each "\n" like "\\n". So, when your XML string is read and stored, all occurrences of "\n" are being escaped to preserve that text as literal text.
当您考虑它时,由于 textview 小部件显示具有文字“\n”值的文本,因此它所提供的字符串必须存储每个“\n”,如“\\n”。因此,当您的 XML 字符串被读取和存储时,所有出现的“\n”都会被转义以将该文本保留为文字文本。
Anyway, all you need to do is:
无论如何,您需要做的就是:
fireBody.setText(fireText.replace("\n", "\n"));
Works for me!
为我工作!
回答by Md Abdul Gafur
For line break you use set text view in xml layout this way.
对于换行,您可以通过这种方式在 xml 布局中使用设置文本视图。
Now is the time
\r\n
for all good men to\r\n
come to the aid of their\r\n
party
现在是
\r\n
所有好人\r\n
都来帮助他们的\r\n
党的时候了
回答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')
1) 使用您需要过滤转义换行符的替换方法(例如'\\n')
2) only then each instance of line feed '\n' escape chars gets rendered into the actual linefeed
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 Corbella
I also had this problem and I found a really SIMPLE SOLUTION, if setting the text from an XML resources (I know this is not the same as OP, but its worth to know it)
我也遇到了这个问题,我发现了一个非常简单的解决方案,如果从 XML 资源设置文本(我知道这与 OP 不同,但值得了解)
There is no need to add line breaks manually, just add the text as it is BUT quoted.
无需手动添加换行符,只需将文本添加为 BUT引用即可。
<string name="test_string">"1. First
2. Second Line
3. Third Line"</string>
回答by Mohsin Naeem
try this
尝试这个
fireBody.setText(Html.fromHtml("testing<br>line<\br>")
your server need to send FirstLine<br>lineAfterLineBreak<\br>
您的服务器需要发送 FirstLine<br>lineAfterLineBreak<\br>
回答by Swayam
To force a line break through the XML of your textview, you need to use \r\ninstead of just \n.
要强制通过 textview 的 XML 换行,您需要使用\r\n而不仅仅是\n。
So, now your code in the XML becomes
所以,现在你在 XML 中的代码变成了
android:text="Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party"
Or if you want to do it programatically, then in your java code :
或者,如果您想以编程方式执行此操作,则在您的 Java 代码中:
fireBody.setText("Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party");
You can also declare the text as a string resource value, like this :
您还可以将文本声明为字符串资源值,如下所示:
<string name="sample_string">"some test line 1 \n some test line 2"</string>
Another easy way to do it would be to change the default attribute of the TextView in your xml file.
另一种简单的方法是更改 xml 文件中 TextView 的默认属性。
<TextView
android:id="@+id/tvContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
/>
And then use, textView.setText("First line \nSecond line \nThird line");
然后使用, textView.setText("First line \nSecond line \nThird line");