为什么要使用strings.xml? [英] Why to use strings.xml?

查看:107
本文介绍了为什么要使用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-frstrings.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屋!

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