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


当前回答

您可以使用这种格式

<string name="test">
"This is
a new line"
</string>

其他回答

很简单,你只要把 \ n 在字符串资源中任何你想要断行的地方。

例如

字符串资源有n个换行符;

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

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

回答晚了,但我想可能会对某人有所帮助。

你有时会在android工作室的布局预览的文本视图中看到\n。(但那是谎言)。请刷新布局。 或者将你的应用部署到一个真正的Android设备上,看看\n在Android字符串资源中实际工作。

\n tag worked in the strings.xml file

Eg:

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

输出:

Hello World &
Welcome

注意:

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

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

我使用了<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>