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

发布于 2024-12-11 15:12:21 字数 200 浏览 0 评论 0原文

我有几种情况,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

许仙没带伞 2024-12-18 15:12:21

两种可能性:

1. 使用源代码,Luke

XML 允许在字符串中使用文字换行符:

<string name="breakfast">eggs
and
spam</string>

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

2. 使用实际的文本文件

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

您可以使用 AssetManager.open() 访问这些资产的文件输入流,使用 Resources.getAssets() 访问 AssetManager 实例,并且...你知道吗,下面是针对这样一个简单任务的 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();
}

使用 Scanner 显然是一个快捷方式 m(

Two possibilities:

1. Use the Source, Luke

XML allows literal newline characters in strings:

<string name="breakfast">eggs
and
spam</string>

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(), a AssetManager instance with Resources.getAssets(), and… you know what, here’s the Java-typical enormously verbose code for such a simple task:

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 shortcut m(

错々过的事 2024-12-18 15:12:21

当然,您可以将换行符放入 XML 中,但这不会给您带来换行符。在 strings.xml 中,与所有 XML 文件一样,字符串内容中的换行符会转换为空格 。因此,声明

<string name="breakfast">eggs
and
spam</string>

一样呈现

eggs and spam

将像在 TextView 中 。幸运的是,有一种简单的方法可以在源代码和输出中包含换行符 - 使用 \n 作为您想要的输出换行符,并转义源代码中真正的换行符。上述声明

<string name="breakfast">eggs\n
and\n
spam</string>

变为

eggs
and
spam

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

<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

<string name="breakfast">eggs\n
and\n
spam</string>

which is rendered as

eggs
and
spam
鲸落 2024-12-18 15:12:21

对于任何正在寻找允许 XML 字符串内容具有多行以实现可维护性并在 TextView 输出中呈现这些多行的可行解决方案的人,只需在新行的开头添加 \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 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.

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:

<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.
凉世弥音 2024-12-18 15:12:21

您可以轻松地使用“”并编写任何单词,即使是其他语言也不会出现错误:

"Hello
世界!
斯拉姆
哎呀!”

You may easily use "" and write any word even from other languages with out error:

<string name="Hello">"Hello
world!
سلام
دنیا!"
</string>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文