字符串资源换行 /n 不可能吗?
似乎不可能向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
使用黑斜杠而不是正斜杠。
\n
另外,如果您计划将字符串用作 HTML,则可以使用
<br />
进行换行(< ;br/>
)use a blackslash not a forwardslash.
\n
Also, if you plan on using the string as HTML, you can use
<br />
for a line break(<br />
)我知道这是一个很老的问题,但当我搜索时它名列前茅。所以我想用另一种方法来更新。
在 strings.xml 文件中,您可以执行 \n 或者只需按 Enter 键:
这将在您的 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:
This will result in the following on your TextView:
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.
在我尝试下一个解决方案后,
\n
""
后添加不带 空格 的\n
lines=2
解决我的是
br
标签更新
最可靠的解决方案是使用翻译编辑器并编辑文本,它将为您处理新行
After I tried next solution
\n
\n
without spaces after it""
and press Enter inside textlines=2
What solves me is
br
tagUpdate
the most reliable solution is to use Translation editor and edit text and it will handle new lines for you
这是一个老问题,但我发现当您创建这样的字符串时:
应用程序中的输出将是这样的(没有换行符)
当您将字符串放在引号中时,
将出现换行符:
This is an old question, but I found that when you create a string like this:
The output in your app will be like this (no newline)
When you put the string in quotation marks
the newline will appear:
在 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.在最新版本的 Android studio 中,“\n”将像它本来应该在那里一样被打印,除非整个字符串都用撇号括起来
例如:
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:
我尝试了两种选择,都很有效且简单。
HTML 标签并使用“\n”换行
例子
I have tried two options, both are efficient and easy.
<br/>
HTML tagand use "\n" to break line
Example
如果你把“\n”放在xml文件的字符串中,它会被视为“\\n”
所以,我这样做了:
然后我在屏幕上看到一个跳行
If you put "\n" in a string in the xml file, it's taken as "\\n"
So , I did :
And then I get a line jump on the screen
\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.
您可以使用这种格式
You can use this format
我刚刚遇到这个问题。
不适用于带有约束参数的
TextView
。添加android:lines="2"
似乎可以解决这个问题。I just faced this issue.
didn't work on
TextView
with constraint parameters. Addingandroid:lines="2"
seems to fix this.我已经尝试过
1-顶部单词
\n
底部单词2-顶部单词 ENTER 底部单词
不起作用
最后
在 android studio 中为我工作事实上,正确的使用方法是 \n 尽管可以在构建应用程序后看到它完成。
仅对设计有影响,对应用程序没有影响
I have tried
1-Top word
\n
Bottom word2-Top word ENTER Bottom word
Didn't work
And finally
<br/>
worked for me in android studioIn 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
最后,我找到了解决方案。
仅适用于设计,不适用于运行应用程序。\n
仅适用于运行应用程序,不适用于运行应用程序设计因此,如果您想获得两个结果,解决方案是同时使用两者。
像这样的
示例:
设计模式的结果:
你好\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 designso the solution is to use both if you want to have both results.
something like this
Example:
Result in design mode:
Hello\n
World.
Result in running app:
Hello
World.
非常简单,你只需要把
\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;
我想扩展这个答案。
他们的意思是这个图标:
它打开一个“真正的编辑器窗口”,而不是大概述中功能有限的文本框。在该编辑器窗口中,允许使用特殊字符、换行符等,并在保存时将其转换为正确的 xml“代码”
I want to expand on this answer.
What they meant is this icon:
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
只需在 strings.xml 文件中使用“\n”,如下所示
即使布局上看起来不是 2 行,但实际上它是 2 行。首先,您可以在翻译编辑器上检查它
单击向下按钮,您将看到此图像
此外,如果您运行该应用程序,您将看到它是用两行编写的。
Just use "\n" in your strings.xml file as below
Even if it doesn't looks 2 lines on layout actually it is 2 lines. Firstly you can check it on Translation Editor
Click the down button and you will see this image
Moreover if you run the app you will see that it is written in two lines.
例如:
输出:
注意:
\n
" 前后添加空格Eg:
Output:
Note:
\n
"要在设计选项卡中查看换行符以及在我使用的设备中查看换行符:(
至 2 条换行符)
To see in the design tab the line breaks and in the device i use:
(to 2 break lines)
尽管实际的问题是对于那些使用反斜杠的人来说仍然存在正斜杠,但在他们的布局中仍然看到 \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.
回答晚了,但我认为可能对某人有帮助。
有时您会在 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.
您是否尝试过将其关闭然后再次打开?!
这是一个非常古老的问题,但似乎没有其他答案表明这一点。
可能是 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.