Android XML 格式中的换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2877828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:41:54  来源:igfitidea点击:

Line Break in XML formatting?

xmlandroidlinebreak

提问by Oliver Goossens

when editing a String in XML I need to add line breaks. And I wanted to ask what is the RIGHT form when programming for android? Because <br>works but ECLIPSE marks the area as problematic. If I check out suggestions Eclipse tells me that I shall add a end tag </br>- IF I add that the line break dissapears...

在 XML 中编辑字符串时,我需要添加换行符。我想问一下在为android编程时什么是正确的形式?因为<br>有效,但 ECLIPSE 将该区域标记为有问题。如果我查看 Eclipse 告诉我的建议,我将添加一个结束标记</br>- 如果我添加换行符消失......

So the one works but is marked as problematic, the other works not but Eclipse tells me its ok..

所以一个有效,但被标记为有问题,另一个无效,但 Eclipse 告诉我它没问题..

What form shall I use?

我应该使用什么表格?

回答by Christopher Orr

Use \nfor a line break and \tif you want to insert a tab.

用于\n换行以及\t是否要插入制表符。

You can also use some XML tags for basic formatting: <b>for bold text, <i>for italics, and <u>for underlined text.

您还可以将一些 XML 标记用于基本格式:<b>用于粗体文本、<i>斜体和<u>带下划线的文本。

Other formatting options are shown in this article on the Android Developers' site:
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

其他格式选项显示在 Android 开发者网站上的这篇文章中:https:
//developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

回答by Sergio Abreu

If you are refering to res strings, use CDATA with \n.

如果要引用 res 字符串,请使用带有 \n 的 CDATA。

<string name="about">
    <![CDATA[
 Author: Sergio Abreu\n
 http://sites.sitesbr.net
  ]]>        
</string>

回答by mobiledev Alex

Also you can add <br> instead of \n.

您也可以添加 <br> 而不是 \n。

And then you can add text to TexView:

然后你可以向 TexView 添加文本:

articleTextView.setText(Html.fromHtml(textForTextView));

回答by Azurespot

Take note: I have seen other posts that say &#xA;will give you a paragraph break, which oddly enough works in the Android xml String.xmlfile, but will NOT show up in a device when testing (no breaks at all show up). Therefore, the \nshows up on both.

请注意:我已经看到其他帖子说&#xA;会给你一个段落中断,这在 Android xmlString.xml文件中很奇怪,但在测试时不会出现在设备中(根本没有出现中断)。因此,\n两者都显示出来。

回答by Lorne Laliberte

Here is what I use when I don't have access to the source string, e.g. for downloaded HTML:

这是我无法访问源字符串时使用的内容,例如下载的 HTML:

// replace newlines with <br>
public static String replaceNewlinesWithBreaks(String source) {
    return source != null ? source.replaceAll("(?:\n|\r\n)","<br>") : "";
}

For XML you should probably edit that to replace with <br/>instead.

对于 XML,您可能应该编辑它以替换为<br/>

Example of its use in a function (additional calls removed for clarity):

它在函数中的使用示例(为清楚起见,删除了其他调用):

// remove HTML tags but preserve supported HTML text styling (if there is any)
public static CharSequence getStyledTextFromHtml(String source) {
    return android.text.Html.fromHtml(replaceNewlinesWithBreaks(source));
}

...and a further example:

...还有一个例子:

textView.setText(getStyledTextFromHtml(someString));