eclipse 如何在 Android strings.xml 文件中编辑多行字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7860603/
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
How to edit multiline strings in Android strings.xml file?
提问by user387184
I have several cases where my string in strings.xml is quite long and has multiple linesdone with \n.
我有几个情况下,我在strings.xml中的字符串是很长,有多条线路与实现\n.
Editing however is quite annoying since it is a long line in Eclipse.
然而,编辑很烦人,因为它在 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?
有没有更好的方法来编辑它,使它看起来稍后会出现在 textview 中,即将换行符作为换行符和多行编辑模式中的文本?
回答by flying sheep
Two possibilities:
两种可能:
1. Use the Source, Luke
1. 使用来源,卢克
XML allows literal newline characters in strings:
XML 允许字符串中的文字换行符:
<string name="breakfast">eggs
and
spam</string>
You just have to edit the XML code instead of using the nifty Eclipse GUI
您只需要编辑 XML 代码而不是使用漂亮的 Eclipse GUI
2. Use actual text files
2. 使用实际的文本文件
Everything inside the assets
directory is available as a input stream from the application code.
assets
目录中的所有内容都可以作为来自应用程序代码的输入流。
You can access those file input streams of assets with AssetManager.open()
, a AssetManager
instance with Resources.getAssets()
, and… you know what, here's the Java-typical enormously verbose code for such a simple task:
您可以使用 访问这些文件输入资产流,使用AssetManager.open()
的AssetManager
实例Resources.getAssets()
,并且……您知道吗,这是用于此类简单任务的 Java 典型的极其冗长的代码:
View view;
//before calling the following, get your main
//View from somewhere and assign it to "view"
String getAsset(String fileName) throws IOException {
AssetManager am = view.getContext().getResources().getAssets();
InputStream is = am.open(fileName, AssetManager.ACCESS_BUFFER);
return new Scanner(is).useDelimiter("\Z").next();
}
the use of Scanner
is obviously a shortcutm(
的使用Scanner
显然是一个快捷方式m(
回答by LightStruk
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
当然,您可以将换行符放入 XML,但这不会给您换行符。在strings.xml 中,与在所有 XML 文件中一样,字符串内容中的换行符被转换为空格。因此,声明
<string name="breakfast">eggs
and
spam</string>
will be rendered as
将呈现为
eggs and spam
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
在一个文本视图中。幸运的是,有一种简单的方法可以在源代码和输出中使用换行符 - 使用 \n 作为您预期的输出换行符,并在源代码中转义真正的换行符。上面的声明变成
<string name="breakfast">eggs\n
and\n
spam</string>
which is rendered as
呈现为
eggs
and
spam
回答by Groove
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... notat 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:
对于正在寻找允许 XML 字符串内容具有多行以实现可维护性并在 TextView 输出中呈现这些多行的工作解决方案的任何人,只需将 a\n
放在新行的开头......而不是在前一行的末尾。正如已经提到的,XML 资源内容中的一个或多个新行被转换为一个空白空间。前导、尾随和多个空格将被忽略。这个想法是将空白放在上一行的末尾,并将 放在\n
下一行内容的开头。这是一个 XML 字符串示例:
<string name="myString">
This is a sentence on line one.
\nThis is a sentence on line two.
\nThis is a partial sentence on line three of the XML
that will be continued on line four of the XML but will be rendered completely on line three of the TextView.
\n\nThis is a sentence on line five that skips an extra line.
</string>
This is rendered in the Text View as:
这在文本视图中呈现为:
This is a sentence on line one.
This is a sentence on line two.
This is a partial sentence on line three of the XML that will be continued on line four of the XML but will be rendered completely on line three of the TextView.
This is a sentence on line five that skips an extra line.
回答by esy
You may easily use "" and write any word even from other languages with out error:
您可以轻松地使用 "" 并使用其他语言编写任何单词而不会出错:
<string name="Hello">"Hello
world!
????
????!"
</string>
<string name="Hello">"Hello
world!
????
????!"
</string>