简体   繁体   English

如何读取string.xml文件中的.txt?

[英]How can I read a .txt in a string.xml file?

I have a long text that I need to put into strings and so far it looks like this: 我有一个长文本需要输入字符串,到目前为止看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="long_text">
xxx
xxx
xxx
xxx
</string>
</resources>

And it looks bad. 而且看起来很糟糕。 Besides, if I want to change the text, it is a bit inconvenient. 此外,如果我想更改文本,这会带来一些不便。 So I would like to have something like this: 所以我想要这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="long_text"> assets/myfile.txt </string>
</resources>

That long xxx text would be stored in a file - myfile.txt - somewhere in the folder. 较长的xxx文本将存储在文件夹中某个文件myfile.txt中。 I've seen some solutions where the text is inserted through the java code, rather than strings.xml, but it is way too complicated for me. 我已经看到了一些通过Java代码而不是strings.xml插入文本的解决方案,但是对我来说太复杂了。 Can anyone help me with this? 谁能帮我这个?

Solution based heavily on the implementation provided by: https://alvinalexander.com/android/how-to-store-static-text-in-android-file-res-values 解决方案主要基于以下提供的实现: https//alvinalexander.com/android/how-to-store-static-text-in-android-file-res-values

You can create a standalone resource outside of the strings.xml file if you're concerned about managing a massive block of text. 如果您担心要管理大量文本,则可以在strings.xml文件之外创建独立资源。 This limits typos to a single file, ie if you forget a bracket somewhere it will isolate to a specific XML document. 这将错别字限制为单个文件,即,如果您忘记了某个地方的方括号,它将被隔离为特定的XML文档。

Create an XML file in res/values called strings_longtext.xml and place the single text value in there. res / values中创建一个名为strings_longtext.xml的XML文件,并将单个文本值放在其中。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="very_large_string">
        <![CDATA[
            Lorem ipsum
            Lorem ipsum
            Lorem ipsum
            Lorem ipsum
            Lorem ipsum
            Lorem ipsum             
        ]]>
    </string>
</resources>

You can then access this string through a context accessor similar to a normal strings.xml file. 然后,您可以通过类似于普通string.xml文件的上下文访问器访问此字符串。

context.getString(R.string.very_large_string);

The reason you should not be putting file names in strings.xml is multifold: 您不应将文件名放在strings.xml中的原因有多种:

  • Changes values of strings as being actual text values to being file references that need to be parsed and processed 将字符串的值作为实际文本值更改为需要解析和处理的文件引用
  • Can be inadvertently broken in i18n efforts through carelessness or automation 可能会由于粗心或自动化而在i18n工作中无意中破坏
  • code/version management exists outside a defined resources directory 代码/版本管理位于已定义的资源目录之外

The purpose of strings files is for pure application-based text; 字符串文件的目的是用于纯基于应用程序的文本。 adding file name interpretation to it is code smell and will cause a painful breakdown as the project gets larger, especially if the files exist outside the standard project structure. 在文件名后面加上文件名解释会产生代码异味,并且随着项目的扩大,会导致痛苦的崩溃,尤其是如果文件存在于标准项目结构之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM