Android 在 xml 文件中写入一些像“<”这样的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3166951/
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 09:00:25  来源:igfitidea点击:

writing some characters like '<' in an xml file

androidxmlspecial-characters

提问by Waza_Be

since the beginning of my programmation, I used some special character like "<-", ""<<" im my string.xml in Eclipse while developping for Android.

自从我开始编程以来,在为 Android 开发时,我在 Eclipse 中使用了一些特殊字符,如“<-”、“”<<”即我的 string.xml。

All worked fine for one year, but today, i just wanted to make some minor changes and began to edit my xml files.

一年来一切正常,但今天,我只想做一些小改动并开始编辑我的 xml 文件。

I get now compilation error on these characters because eclipse believe it's part of the xml blocks.

我现在在这些字符上遇到编译错误,因为 eclipse 相信它是 xml 块的一部分。

Any idea on how I could add this symbol "<" in my xml files?

关于如何在我的 xml 文件中添加这个符号“<”的任何想法?

Thank a lot.

非常感谢。

回答by reece

Use

&lt;for <

&lt;为了 <

&gt;for >

&gt;为了 >

&amp;for &

&amp;为了 &

回答by NguyenDat

Another way to insert special character follow Mossguide: How can I write character & in android strings.xmlby used Unicode definition:

另一种插入特殊字符的方法遵循Moss指南:How can I write character & in android strings.xmlby used Unicode definition

Example:

例子:

<string name="item_unknown">\u003c Item Unknown \u003e</string>

which present in string :

存在于字符串中:

< Item Unknown >

回答by Xebozone

I stumbled upon this question, as I use HTML markup in my strings.

我偶然发现了这个问题,因为我在字符串中使用了 HTML 标记。

If you refer to the Android String Resources documentation, in the section: "Styling with HTML markup" you will see that in strings.xml, you need to use &lt; for an opening bracket, but you can safely use backslash and a closing bracket. For example:

如果您参考 Android字符串资源文档,在“使用 HTML 标记设置样式”一节中,您将看到在 strings.xml 中,您需要使用 < 对于左括号,但您可以安全地使用反斜杠和右括号。例如:

<b>Text</b>

can be written as:

可以写成:

&lt;b>Text&lt;/b>

in your strings.xml.

在你的strings.xml 中。

If using this HTML string in your code, you can use:

如果在您的代码中使用此 HTML 字符串,您可以使用:

Html.fromHtml( getResources().getString(R.string.yourHTMLString ) 

and the output will be your bold string!

输出将是您的粗体字符串!