如何在Android strings.xml 文件中编辑多行字符串?
我有几种情况,strings.xml 中的 字符串相当长,并且有多行 使用 \n 完成。
然而,编辑非常烦人,因为它在 Eclipse 中是很长的一行。
是否有更好的方法来编辑它,使其看起来稍后会在文本视图中呈现,即换行符作为换行符,文本处于多行编辑模式?
I have several cases where my string in strings.xml is quite long and has multiple lines done with \n.
Editing however is quite annoying since it is a long line in Eclipse.
Is there a better way to edit this so it looks like it will be presented later in the textview, ie the line breaks as line breaks and the text in multiline edit mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两种可能性:
1. 使用源代码,Luke
XML 允许在字符串中使用文字换行符:
您只需编辑 XML 代码,而不是使用漂亮的 Eclipse GUI
2. 使用实际的文本文件
assets
目录中的所有内容都可以作为应用程序代码的输入流。您可以使用
AssetManager.open()
访问这些资产的文件输入流,使用Resources.getAssets()
访问AssetManager
实例,并且...你知道吗,下面是针对这样一个简单任务的 Java 典型的极其冗长的代码:使用
Scanner
显然是一个快捷方式 m(Two possibilities:
1. Use the Source, Luke
XML allows literal newline characters in strings:
You just have to edit the XML code instead of using the nifty Eclipse GUI
2. Use actual text files
Everything inside the
assets
directory is available as a input stream from the application code.You can access those file input streams of assets with
AssetManager.open()
, aAssetManager
instance withResources.getAssets()
, and… you know what, here’s the Java-typical enormously verbose code for such a simple task:the use of
Scanner
is obviously a shortcut m(当然,您可以将换行符放入 XML 中,但这不会给您带来换行符。在 strings.xml 中,与所有 XML 文件一样,字符串内容中的换行符会转换为空格 。因此,声明
一样呈现
将像在 TextView 中 。幸运的是,有一种简单的方法可以在源代码和输出中包含换行符 - 使用 \n 作为您想要的输出换行符,并转义源代码中真正的换行符。上述声明
变为
Sure, you could put newlines into the XML, but that won't give you line breaks. In strings.xml, as in all XML files, Newlines in string content are converted to spaces. Therefore, the declaration
will be rendered as
in a TextView. Fortunately, there's a simple way to have newlines in the source and in the output - use \n for your intended output newlines, and escape the real newlines in the source. The above declaration becomes
which is rendered as
对于任何正在寻找允许 XML 字符串内容具有多行以实现可维护性并在 TextView 输出中呈现这些多行的可行解决方案的人,只需在新行的开头添加
\n
即可...不在上一行的末尾。正如已经提到的,XML 资源内容中的一个或多个新行将转换为一个空格。前导、尾随和多个空格将被忽略。这个想法是将空白放在上一行的末尾,并将\n
放在下一行内容的开头。这是一个 XML 字符串示例:在文本视图中呈现为:
For anyone looking for a working solution that allows the XML String content to have multiple lines for maintainability and render those multiple lines in TextView outputs, simply put a
\n
at the beginning of the new line... not at the end of the previous line. As already mentioned, one or more new lines in the XML resource content is converted to one single empty space. Leading, trailing and multiple empty spaces are ignored. The idea is to put that empty space at the end of the previous line and put the\n
at the beginning of the next line of content. Here is an XML String example:This is rendered in the Text View as:
您可以轻松地使用“”并编写任何单词,即使是其他语言也不会出现错误:
世界!
斯拉姆
哎呀!”
You may easily use "" and write any word even from other languages with out error:
<string name="Hello">"Hello
world!
سلام
دنیا!"
</string>