为什么要使用strings.xml? [英] Why to use strings.xml?
问题描述
任何人都可以解释为什么使用硬编码字符串是如此糟糕吗? 如果我对字符串进行硬编码,应该期待什么问题? 为什么拥有一个单独的字符串xml文件可以解决这些问题?
Could anyone please explain why having hard-coded strings is so bad? What issues should I expect if I hardcode strings? Why having a separate string xml file solves those issues?
在strings.xml中创建新元素非常耗时.
Creating new elements in strings.xml is such a time consuming activity.
推荐答案
将字符串添加到strings.xml
时,您可以轻松地将整个应用翻译成其他语言.
When adding your strings to strings.xml
, you can easily translate your whole app into other languages.
因此,在文件夹values
中,您将具有strings.xml
并包含以下内容:
So in the folder values
you would have strings.xml
with this content:
<string name="hello">Hello</string>
在values-fr
和strings.xml
中具有以下内容:
<string name="hello">Bonjour</string>
这为您节省了 很多 的工作,而无需执行硬编码的方法:Android会根据用户的偏好自动选择正确的语言,而您不必担心选择和显示这种语言.
This saves you a lot of work instead of doing this the hardcoded way: Android automatically selects the correct language based on user preferences, and you don't have to worry about selecting and displaying this language.
还请阅读本地化| Android开发人员.
这篇关于为什么要使用strings.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!