在XML资源字符串中添加新的行/n似乎是不可能的。还有别的办法吗?


当前回答

如果你在xml文件的字符串中输入“\n”,它会被当作“\\n”

所以,我做了:

text = text.Replace("\\\n", "\n");   ( text is taken from resX file) 

然后我在屏幕上有一个跳转

其他回答

使用黑斜线而不是正斜线。\ n

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello\nWorld!</string>
</resources>

同样,如果你计划使用字符串作为HTML,你可以使用&lt;br /&gt;对于换行符(<br />)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello&lt;br /&gt;World!</string>
</resources>

我试过了 1-顶部单词\n底部单词 2顶部单词ENTER底部单词

没有工作 最后<br/>为我在android studio工作

事实上,正确的使用方法是\n,尽管在构建应用程序之后可能会看到它完成。只对设计有影响,对应用没有影响

<string name="available_balance">Available\nBalance</string> //Look good on the app and Available\nBalance in android studio
<string name="available_balance">Available<br/>Balance</string> //Look good in android studio and Available Balance on the App
\n tag worked in the strings.xml file

Eg:

 <string name="hello">Hello World &amp;\nWelcome</string>

输出:

Hello World &
Welcome

注意:

请确保您没有在\n的前后添加空格。” 当你运行应用程序时,即使它不在UI预览窗口的新行上显示,它也会在下一行中显示。

我想进一步解释这个问题。 他们的意思是这个图标:

它打开了一个“真正的编辑器窗口”,而不是大概览中的有限功能文本框。在该编辑器窗口中,允许特殊字符、换行符等,并在保存时转换为正确的xml“代码”

这是一个老问题,但我发现当你创建一个这样的字符串时:

<string name="newline_test">My
New line test</string>

在你的应用程序中的输出将是这样的(没有换行)

My New line test

当你把字符串放在引号里

<string name="newline_test">"My
New line test"</string>

换行符将出现:

My 
New line test