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


当前回答

在最新版本的Android studio中,"\n"会像它应该在那里一样被打印出来除非整个字符串是撇号

例如:

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

其他回答

不要在\n后面加空格,否则行不通。我不知道原因,但这招对我很管用。

在最新版本的Android studio中,"\n"会像它应该在那里一样被打印出来除非整个字符串是撇号

例如:

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

我尝试了两种选择,都是有效和简单的。

我使用了<br/> HTML标签

<string name="name_detail">
        Hey user. <br/>
        Enjoy a new shopping deal.!!
</string>

在strings.xml的顶部添加编码行

<?xml version="1.0" encoding="utf-8"?>

然后用“\n”换行 例子

<string name="name_detail">
        Hey user.\n
        Enjoy a new shopping deal.!!
</string>

当在Android Studio中使用翻译编辑器时,只需单击右边的图标(在Windows中使用Shift-Enter,在MacOS中使用Alt/Option-Enter),然后使用return添加换行符。

这将在本地化的strings.xml中正确地插入\n。

\n tag worked in the strings.xml file

Eg:

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

输出:

Hello World &
Welcome

注意:

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