Android TextView不支持换行符 [英] Android textview not supporting line break

查看:681
本文介绍了Android TextView不支持换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建一个自定义视图,该视图显示从XML文件解析的文本.该文本很长,并且包含用于强制换行符的"/n"字符.由于某种原因,文本视图显示/n,并且没有任何换行符.这是我的代码:

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:

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

XML文件中的文本是这样的:

The text from the XML file is like this:

Now is the time /n for all good men to /n come to the aid of their /n party

它应该这样显示;

Now is the time
for all good men to
come to the aid of their
party

有没有我想念的设置?

UPDATE

UPDATE

\ r \ n硬编码到视图中,则可以使用.即:

\r\n works if I hard code it into my view. 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:

String fireText = "Line one\nLine two\nLine three";

仅供参考

FYI

System.getProperty("line.separator");

这将返回字符串"/n",因此无需转换为"/r/n".

this returns a string of "/n" so there is no need to convert to "/r/n".

不幸的是,我的数据起源于一个XML文件,该文件被解析并存储在哈希图中.我尝试了以下方法:

Unfortunately, my data originates in a XML file that is parsed and stored in a hashmap. I tried the following:

String fireText = body1.toString().replaceAll("\n", "\r\n");

\ n不会被\ r \ n取代.可能是因为我正在从对象转换为String吗?

The \n is not getting replaced with \r\n. Could it be because I'm converting from an object to String?

推荐答案

在相同的环境下,我一直遇到相同的问题.解决方案相当简单.

I've been having the exact same problem under the exact same circumstances. The solution is fairly straight forward.

考虑它时,由于textview小部件正在显示带有文字"\ n"值的文本,因此所给出的字符串必须像"\\ n"一样存储每个"\ n".因此,当读取并存储您的XML字符串时,所有出现的\ n都将被转义以将该文本保留为原义文本.

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.

无论如何,您要做的就是:

Anyway, all you need to do is:

fireBody.setText(fireText.replace("\\n", "\n"));

为我工作!

这篇关于Android TextView不支持换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆