如何在Android strings.xml文件中编辑多行字符串?

use*_*184 11 eclipse string android multiline

我有几种情况,我在strings.xml中的字符串很长,有多行完成\n.

然而编辑非常烦人,因为它在Eclipse中很长.

有没有更好的方法来编辑它,所以看起来它将在稍后的textview中显示,即换行符换行符和多行编辑模式中的文本?

fly*_*eep 14

两种可能性:

1.使用Source,Luke

XML允许字符串中的文字换行符:

<string name="breakfast">eggs
and
spam</string>
Run Code Online (Sandbox Code Playgroud)

您只需编辑XML代码而不是使用漂亮的Eclipse GUI

2.使用实际的文本文件

assets目录中的所有内容都可用作应用程序代码的输入流.

你可以访问那些资产的文件输入流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();
}
Run Code Online (Sandbox Code Playgroud)

使用Scanner 显然是捷径 m(


Lig*_*ruk 11

当然,您可以将新行添加到XML中,但这不会给您换行.在strings.xml中,与所有XML文件一样,字符串内容中的换行符将转换为空格.因此,申报

<string name="breakfast">eggs
and
spam</string>
Run Code Online (Sandbox Code Playgroud)

将呈现为

eggs and spam
Run Code Online (Sandbox Code Playgroud)

在TextView中.幸运的是,有一种简单的方法可以在源代码和输出中使用换行符 - 使用\n作为预期的输出换行符,并转义源代码中的实际换行符.上述声明成为

<string name="breakfast">eggs\n
and\n
spam</string>
Run Code Online (Sandbox Code Playgroud)

呈现为

eggs
and
spam
Run Code Online (Sandbox Code Playgroud)


小智 6

对于正在寻找一种允许XML String内容具有多行以保持可维护性并在TextView输出中呈现这些多行的解决方案的任何人,只需在新行\n开头 ... 而不是前一行的末尾放置a即可。如前所述,XML资源内容中的一个或多个新行将转换为一个空白空间。前导,尾随和多个空白将被忽略。想法是将空白区域放在上一行的末尾,将内容放在\n下一行的开头。这是一个XML String示例:

<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>
Run Code Online (Sandbox Code Playgroud)

这在“文本视图”中呈现为:

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.
Run Code Online (Sandbox Code Playgroud)