字符串资源换行 /n 不可能吗?

发布于 2024-10-27 11:32:49 字数 61 浏览 2 评论 0原文

似乎不可能向 XML 资源字符串添加新行 /n 。还有另一种方法可以做到这一点吗?

It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

匿名的好友 2024-11-03 11:32:50

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

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

另外,如果您计划将字符串用作 HTML,则可以使用 <br /> 进行换行(< ;br/>)

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

use a blackslash not a forwardslash. \n

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

Also, if you plan on using the string as HTML, you can use <br /> for a line break(<br />)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello<br />World!</string>
</resources>
半世蒼涼 2024-11-03 11:32:50

我知道这是一个很老的问题,但当我搜索时它名列前茅。所以我想用另一种方法来更新。

在 strings.xml 文件中,您可以执行 \n 或者只需按 Enter 键:

这是你的字符串。

   这是字符串的第二行。\n\n 字符串的第三行。

这将在您的 TextView 上产生以下结果:

这是你的字符串。

这是字符串的第二行。

字符串的第三行。

这是因为在字符串的开始声明和新行之间有两次回车。为了清楚起见,我还在其中添加了 \n,因为两者都可以使用。我喜欢在 xml 中使用回车符来查看列表或我拥有的任何多行字符串。我的两分钱。

I know this is pretty old question but it topped the list when I searched. So I wanted to update with another method.

In the strings.xml file you can do the \n or you can simply press enter:

<string name="Your string name" > This is your string.

   This is the second line of your string.\n\n Third line of your string.</string>

This will result in the following on your TextView:

This is your string.

This is the second line of your string.

Third line of your string.

This is because there were two returns between the beginning declaration of the string and the new line. I also added the \n to it for clarity, as either can be used. I like to use the carriage returns in the xml to be able to see a list or whatever multiline string I have. My two cents.

金兰素衣 2024-11-03 11:32:50

在我尝试下一个解决方案后,

  • 添加 \n
  • 在使用 "" 后添加不带 空格\n
  • 并按 输入内部文本
  • 文本,按Enter
  • 使用lines=2

解决我的是br标签

<string name="successfullyfeatured">successfully<br/> You are now a member</string>

更新

最可靠的解决方案是使用翻译编辑器并编辑文本,它将为您处理新行

After I tried next solution

  • add \n
  • add \n without spaces after it
  • use "" and press Enter inside text
  • text with press Enter
  • use lines=2

What solves me is br tag

<string name="successfullyfeatured">successfully<br/> You are now a member</string>

Update

the most reliable solution is to use Translation editor and edit text and it will handle new lines for you

天赋异禀 2024-11-03 11:32:50

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

<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

This is an old question, but I found that when you create a string like this:

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

The output in your app will be like this (no newline)

My New line test

When you put the string in quotation marks

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

the newline will appear:

My 
New line test
痴者 2024-11-03 11:32:50

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

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

When using the translations editor in Android Studio just click the icon to the right (or use Shift-Enter on Windows, Alt/Option-Enter on MacOS) and then add line breaks using return.

This will insert \n correctly in the localized strings.xml.

始终不够 2024-11-03 11:32:50

在最新版本的 Android studio 中,“\n”将像它本来应该在那里一样被打印,除非整个字符串都用撇号括起来

例如:

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

In the latest version of Android studio, "\n" is going to be printed like it was meant to be there unless the whole string it's in apostrophes

For Example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">"Hello\nWorld!"</string>
</resources>
酒解孤独 2024-11-03 11:32:50

我尝试了两种选择,都很有效且简单。

  1. 我使用
    HTML 标签
<string name="name_detail">
        Hey user. <br/>
        Enjoy a new shopping deal.!!
</string>
  1. 在 strings.xml 顶部添加编码行
<?xml version="1.0" encoding="utf-8"?>

并使用“\n”换行
例子

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

I have tried two options, both are efficient and easy.

  1. I used <br/> HTML tag
<string name="name_detail">
        Hey user. <br/>
        Enjoy a new shopping deal.!!
</string>
  1. Add an encoding line at the top of strings.xml
<?xml version="1.0" encoding="utf-8"?>

and use "\n" to break line
Example

<string name="name_detail">
        Hey user.\n
        Enjoy a new shopping deal.!!
</string>
初与友歌 2024-11-03 11:32:50

如果你把“\n”放在xml文件的字符串中,它会被视为“\\n”

所以,我这样做了:

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

然后我在屏幕上看到一个跳行

If you put "\n" in a string in the xml file, it's taken as "\\n"

So , I did :

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

And then I get a line jump on the screen

何以笙箫默 2024-11-03 11:32:50

\n后面不要加空格,否则不起作用。我不知道原因,但这个技巧对我来说非常有效。

don't put space after \n, otherwise, it won't work. I don't know the reason, but this trick worked for me pretty well.

灼疼热情 2024-11-03 11:32:50

您可以使用这种格式

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

You can use this format

<string name="test">
"This is
a new line"
</string>
风吹过旳痕迹 2024-11-03 11:32:50

我刚刚遇到这个问题。
不适用于带有约束参数的 TextView。添加 android:lines="2" 似乎可以解决这个问题。

I just faced this issue.
didn't work on TextView with constraint parameters. Adding android:lines="2" seems to fix this.

二智少女 2024-11-03 11:32:50

我已经尝试过
1-顶部单词 \n 底部单词
2-顶部单词 ENTER 底部单词

不起作用
最后
在 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

I have tried
1-Top word \n Bottom word
2-Top word ENTER Bottom word

Didn't work
And finally <br/> worked for me in android studio

In fact the correct way to use it is \n although it is possible to see it done after Build the application. The
only has an effect on the design and not on the application

<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
似最初 2024-11-03 11:32:50

最后,我找到了解决方案。

仅适用于设计,不适用于运行应用程序。
\n 仅适用于运行应用程序,不适用于运行应用程序设计
因此,如果您想获得两个结果,解决方案是同时使用两者。
像这样的

\n<br/>

示例:

Hello \n<br/>World.

设计模式的结果:
你好\n
世界。

运行应用程序的结果:
你好
世界。

finally, I found the solution.
the <br/> is working on design only and it's not working in running app.
the \n is working on running app only and not working on design
so the solution is to use both if you want to have both results.
something like this

\n<br/>

Example:

Hello \n<br/>World.

Result in design mode:
Hello\n
World.

Result in running app:
Hello
World.

一抹微笑 2024-11-03 11:32:50

非常简单,你只需要把
\n
您想在字符串资源中的任何位置换行。

例如

String s = 我的字符串资源此处有 \n 换行符;

Very simple you have to just put
\n
where ever you want to break line in your string resource.

For example

String s = my string resource have \n line break here;

爱的故事 2024-11-03 11:32:50

我想扩展这个答案。
他们的意思是这个图标:

编辑器窗口按钮

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

I want to expand on this answer.
What they meant is this icon:

Editor window button

It opens a "real editor window" instead of the limited-feature text box in the big overview. In that editor window, special chars, linebreaks etc. are allowed and converted to the correct xml "code" when saved

黯然#的苍凉 2024-11-03 11:32:50

只需在 strings.xml 文件中使用“\n”,如下所示

<string name="relaxing_sounds">RELAXING\nSOUNDS</string>

即使布局上看起来不是 2 行,但实际上它是 2 行。首先,您可以在翻译编辑器上检查它

在此处输入图像描述

单击向下按钮,您将看到此图像

在此处输入图像描述

此外,如果您运行该应用程序,您将看到它是用两行编写的。

Just use "\n" in your strings.xml file as below

<string name="relaxing_sounds">RELAXING\nSOUNDS</string>

Even if it doesn't looks 2 lines on layout actually it is 2 lines. Firstly you can check it on Translation Editor

enter image description here

Click the down button and you will see this image

enter image description here

Moreover if you run the app you will see that it is written in two lines.

酸甜透明夹心 2024-11-03 11:32:50
\n tag worked in the strings.xml file

例如:

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

输出:

Hello World &
Welcome

注意:

  • 请确保在 \n" 前后添加空格
  • 即使在运行应用程序时它不会在 UI 预览窗口中显示为换行,它也会显示在下一行。
\n tag worked in the strings.xml file

Eg:

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

Output:

Hello World &
Welcome

Note:

  • Make sure that you dont add space before and after of \n"
  • Even if it doesnt looks on new line in the preview window of UI when you run the app it will be displayed in the next line.
诗笺 2024-11-03 11:32:50

要在设计选项卡中查看换行符以及在我使用的设备中查看换行符:(

<br/><br/>\n\n

至 2 条换行符)

To see in the design tab the line breaks and in the device i use:

<br/><br/>\n\n

(to 2 break lines)

伤痕我心 2024-11-03 11:32:50

尽管实际的问题是对于那些使用反斜杠的人来说仍然存在正斜杠,但在他们的布局中仍然看到 \n

好吧,一开始我也很困惑为什么 \n 在布局中不起作用,但似乎当您在实际设备中实际看到该布局时,它会创建一个换行符,因此无需担心它在布局显示屏上显示的内容,您的换行符将在设备上完美运行。

Although the actual problem was with the forward slash still for those using backslash but still saw \n in their layout.

Well i was also puzzled at first by why \n is not working in the layout but it seems that when you actually see that layout in an actual device it creates a line break so no need to worry what it shows on layout display screen your line breaks would be working perfectly fine on devices.

情泪▽动烟 2024-11-03 11:32:50

回答晚了,但我认为可能对某人有帮助。

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

Late answer but i think might help someone.

You will some times see \n in the textview in android studio's layout preview. (But it lies then). Please refresh layout.
Or deploy your app to a real Android Device to see the \n actually working in android string resource.

旧情勿念 2024-11-03 11:32:50

您是否尝试过将其关闭然后再次打开?!
这是一个非常古老的问题,但似乎没有其他答案表明这一点。
可能是 android studio 上的布局预览未正确显示 textView。
所以你可以尝试退出 android studio 并再次启动它。
我想如果 android studio 在计算机处于睡眠/休眠等状态时打开,就会发生这种情况。
无论如何,值得一试。

Have you tried turning it off and on again ?!
This is a very old question but no other answer seem to suggest this.
It could be that the layout preview on android studio is not properly displaying the textView.
So you could try exiting android studio and launching it again.
I guess this can happen if android studio was open while the computer was in sleep/hibernate etc.
Anyway, worth a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文