Is it possible to create translateable arbitrary XML resources in Android Studio?(是否可以在Android Studio中创建可翻译的任意XML资源?)
本文介绍了是否可以在Android Studio中创建可翻译的任意XML资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个应用程序,它向用户提问,给答案打分,并可能对他们提出后续问题做出反应。为此,我在res/xml/questions.xml
中想到了类似以下的XML:
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question id="000" category="2">
<text>Yes or no?</text>
<answers>
<choice id="0" score="+5">Yes</choice>
<choice id="1" score="-5">No</choice>
</answers>
</question>
<question id="010" category="1">
<parent id="000" choice="0"/>
<text>Whats my question?</text>
<answers>
<choice id="0" score="-5">Shut up.</choice>
<choice id="1" score="0">I don't care.</choice>
<choice id="2" score="+5">I like your attitude!</choice>
</answers>
</question>
</questions>
我希望支持多种语言。如何翻译<text>
和<choice>
的内容,而不需要在不同的XML中重新定义相同的逻辑?(或者我应该完全放弃XML方法?)
推荐答案
以下是一些选项:
选项1:将res/xml/questions.xml
和该XML的其他变体用于不同的语言(例如,res/xml-es/questions.xml
、res/xml-de/questions.xml
、res/xml-zh/questions.xml
)
res/xml/questions.xml
可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question id="000" category="2">
<text>question_000</text>
<answers>
<choice id="0" score="+5">question_000_choice_0</choice>
<choice id="1" score="-5">question_000_choice_1</choice>
</answers>
</question>
<question id="010" category="1">
<parent id="000" choice="0"/>
<text>question_010</text>
<answers>
<choice id="0" score="-5">question_010_choice_0</choice>
<choice id="1" score="0">question_010_choice_1</choice>
<choice id="2" score="+5">question_010_choice_2</choice>
</answers>
</question>
</questions>
那么您将拥有question_000
、question_000_choice_0
等的字符串资源。解析XML时,然后在Resources
对象上使用getIdentifier()
来查找与question_000_choice_0
之类的内容相对应的字符串资源ID。
选项3:让XML简单地描述基本内容:
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question id="000" category="2">
<answers>
<choice id="0" score="+5" />
<choice id="1" score="-5" />
</answers>
</question>
<question id="010" category="1">
<parent id="000" choice="0"/>
<answers>
<choice id="0" score="-5" />
<choice id="1" score="0" />
<choice id="2" score="+5" />
</answers>
</question>
</questions>
您仍然拥有question_000
、question_000_choice_0
等的字符串资源。但是,您只需从问题ID和选择ID生成这些名称,而不是将这些名称放在XML中。
这篇关于是否可以在Android Studio中创建可翻译的任意XML资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:是否可以在Android Studio中创建可翻译的任意XML资源?


基础教程推荐
猜你喜欢
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01