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

查看:36
本文介绍了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

是否有我遗漏的设置?

更新

如果我将其硬编码到我的视图中,它就可以工作.即:

works if I hard code it into my view. ie:

String fireText = "Now is the time 
 for all good men 
 to come to the aid";

如果我硬编码,实际上 也可以:

Actually also works if i hard code it:

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

仅供参考

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("
", "
");

没有被替换为 .难道是因为我是从一个对象转换成字符串?

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

推荐答案

exact相同的情况下,我一直遇到exact相同的问题.解决方案相当简单.

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

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

When you think about it, since the textview widget is displaying the text with the literal " " values, then the string that it is being given must be storing each " " like "\n". So, when your XML string is read and stored, all occurrences of " " are being escaped to preserve that text as literal text.

无论如何,你需要做的就是:

Anyway, all you need to do is:

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

为我工作!

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

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