自定义XML资源中的引用字符串资源 [英] Reference String Resource in custom XML Resource

查看:67
本文介绍了自定义XML资源中的引用字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android App Project的 assets 文件夹中,我存储了描述游戏中动作的自定义XML文件.由于它们稍微复杂一点,因此无法直接将其编写为Android资源.

In the assets folder of my Android App Project I am storing custom XML files which describe actions in my game. Since they are a little more complex, they can not be directly written as Android Resources.

更新:我现在将自定义(复杂)XML文件存储在 res/xml 文件夹中.

Update: I am storing my custom (complex) XML files in the res/xml folder now.

简单示例:

<dialog>
    <npc>1</npc>
    <text>Hello! How are you?</text>
</dialog>

我想要的是利用Android String资源的便捷优势进行语言本地化.在上面的示例中,我要保存 Hello!您好吗?在我的 res/values/strings.xml 中,然后像这样引用它:

What I want is to use the convenient advantages of Android String resources for language localisation. In the example above, I want to save Hello! How are you? in my res/values/strings.xml and then reference this somehow like this:

<text>@string/dialog_1_text</text>

不想创建其他语言的 res/xml 文件夹,我在其中复制所有xml文件并完全翻译它们.我只想翻译其中的一部分.通过引用String资源,很容易.

I do not want to create different language res/xml folders where I copy all xml files and translate them completely. I only want parts of it to be translated. Easily by referencing a String resource.

更新2:我现在发现,您从 Resource.getXml()获得的XMLParser具有名为 getAttributeResourceValue()的方法.>,它会自动将诸如 randomAtt ="@ string/random_string" 之类的属性转换为实际的资源ID.
但是,在XML文件中,当您放入无效资源时,仍然没有明确的依赖关系,也没有字符串的预览或警告.但是-甚至还有一种方法-我坚信可以让验证器只允许某些自定义属性中的字符串资源.

Update 2: I have now found out that the XMLParser that you get from Resource.getXml() has a method called getAttributeResourceValue() which converts an attribute like randomAtt="@string/random_string" automatically to an actual Resource ID.
However, in the XML file, there still is no clear dependency and there is no preview for the string or a warning when you put in an invalid resource. But - since there even is a method for that - I strongly believe that it is possible to let the validator only allow string resources in some custom attributes.

推荐答案

好的,所以在过去两个小时的大量研究中,我终于找到了一个很好的解决方案.我看到一些线程提出了非常相似的问题-这就是为什么我确定这个答案将来可能会帮助某些程序员的原因.

Okay, so after a lot of research in the past two hours, I finally found a very well working solution. I have seen a few threads which ask very similar questions - that's why I am sure this answer might help some programmers in the future.

要求:
我的任务要求如下:使用我自己的架构在自定义XML文件中引用字符串资源,该文件存储在 res/xml 目录中.Android Studio的XML验证程序应自动将其检测为String资源,并在无效时显示警告-在有效字符串时预览实际的String.另外,它应该尽可能地表现出色.

Requirements:
The requirements for my task were the following: Referencing String Resources in a custom XML file with my own schema, stored in the res/xml directory. The XML Validator of Android Studio should automatically detect it as a String resource, print a warning when it is invalid - and preview the actual String when it is a valid resource. Also it should be as performant as possible.

解决方案:
这就是我解决的方法:
我没有将字符串资源放在标签之间,而是将它们作为属性.就我而言,没什么大不了的.
并且为了使验证者将它们识别为String资源,我不得不从 android 命名空间中调用以下属性 text :更新:我发现您可以随心所欲地调用该属性,而不必包含android名称空间.

Solution:
And this is how I solved it:
Instead of putting the string resource between the tags, I had to put them as an attribute. No big deal in my case.
And for the validator to recognize them as String resource, I had to call these attributes text from the android namespace: Update: I found out that you can call the attribute whatever you like, and it is not necessary to include the android namespace.

<main>
    <nested>
        <test myText="@string/lorem_ipsum_100"/>
    </nested>
</main>

(您的自定义XML可能看起来像您想要/需要的一样!这只是一个示例-它可与无限制的嵌套标签和您自己定义的模式一起使用.)

(Your custom XML may look as whatever you like/need! This is just an example - it works with unlimited nested tags and your own defined schema.)

我认为这不会奏效-但实际上效果很好!每当我点击 Build project 时,当我使用无效的String资源时,它都会打印出警告.
不仅如此,它还可以根据需要预览它:

I didn't think this was gonna work - but it actually does quite well! Whenever I hit Build project, it prints out a warning when I used an invalid String resource.
And not only that, it also previews it as desired:

(注意:此屏幕截图是在我注意到您可以根据需要/需要调用该属性之前制作的.无需显式调用 android:text .AndroidStudio会自动识别该属性作为字符串资源,只要您输入 @string/....)

(Note: This screenshot was made before I noticed that you can call the attribute whatever you want/need. There is no need to call it explicitely android:text. Android Studio will automatically recognize it as a string resource, as long as you put @string/....)

最后但并非最不重要的一点是,为了让您的Java代码正确解释资源,您必须执行以下操作:

Now last but not least, to let your Java code interpret the resource correctly, you have to do this:

XmlResourceParser parser = getResources().getXml(R.xml.tutorial_welcome_dialog);
try {
    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("test")) {
            String s;
            for (int i = 0; i < parser.getAttributeCount(); i++) {
                if (parser.getAttributeName(i).equals("text"))
                    s = getResources().getString(parser.getAttributeResourceValue(i, -1));
            }
        }

    }
} catch (Exception e) {
    e.printStackTrace();
}

性能说明:据我所知,该解决方案也是超高性能的,因为android自动将 @string/...预先解析为资源ID.

Performance note: As far as I can see, this solution is also super performant, since android pre-parses the @string/... automatically into Resource IDs.

这篇关于自定义XML资源中的引用字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆