机器人 - 的strings.xml VS静态常量 [英] android - strings.xml vs static constants

查看:135
本文介绍了机器人 - 的strings.xml VS静态常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在想什么:

在strings.xml中的字符串应该是使用了布局(XML)的使用。和静态常量是codeS(的.java)使用。

当涉及到的最佳做法,我想知道这应该是使用。

如果你有大量的字符串,这会对性能的影响?

 的getString(...)VS MyConstants.THIS_IS_A_CONSTANT
 

解决方案

有两种一些优点和缺点(我应该说优势,少的优势),在这两种情况。

在你的问题的意见,他们说这一切。我只是想添加一些小问题。

本地化:

有关本地化的问题肯定字符串资源是最好的,你可以用不同的语言文件differente语言环境。

内存:

作为字符串资源保存在XML文件中,以便有一些额外的开销(不是一个主要的虽然)

性能:

从内存中读取始终不是从文件读取速度更快。虽然在这种情况下,性能上的差异是不显著

修的:

这仅仅是一个个人的看法。对我来说,保持RES文件比保持字符串类更容易。 string.xml将更加有可读性我​​。

最后:

所以,我的建议是

  

使用字符串资源将被显示给用户的文本。

  

使用静态常量为你的程序的内部puposes像   数据库名称,内部变量,意图筛选器名称等。

Here's what I'm thinking:

The strings on strings.xml should be use for the layouts (xml) to use. And static constants are for codes (.java) to use.

When it comes to best practices, I'd like to know which should be use.

If you have lots of strings, will it have performance effect?

getString(...) vs MyConstants.THIS_IS_A_CONSTANT

解决方案

There are both some advantages and disadvantages ( I should say advantages and less advantages) in these two cases.

as in the comments of your question they said it all. I just want to add some minor points.

Localization:

For localization issue definitely String resource is the best as you can use different language file for differente Locale.

Memory:

As String resources are saved in xml file so there are some extra overhead (not a major one though)

Performance:

reading from memory is always faster than reading from file. Although in this case the performance difference is not significant

Maintainance:

It is just a personal opinion. To me maintaining res file is easier than maintaining string in class. string.xml is more readable to me.

Finally:

So my suggestion is

use string resources for the texts which will be displayed to user.

and

use static constants for internal puposes of your program like database names, internal variable, intent filter name etc.

这篇关于机器人 - 的strings.xml VS静态常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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