在 android 中定义常量的最佳方法是静态类、接口还是 xml 资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11150701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is best way to define constants in android, either static class, interface or xml resource?
提问by Jayabal
I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give application for testing/live. so I planned to make it as configurable, so that application can get appropriate URL based on me build type configuration constant. So,
我正在开发一个 android 应用程序,它使用 Web 服务从服务器获取数据,为此我有三组不同的 URL 指向开发系统、测试服务器和实时服务器。每当我想提供测试/实时应用程序时,都很难更改 URL。所以我计划将其设置为可配置的,以便应用程序可以根据我的构建类型配置常量获得适当的 URL。所以,
- which is the best way to keep this constants, java static class or java public interface or xml resource file.? When? Why?
- which gives better performance?, When? Why?
- 这是保持这个常量的最好方法,java静态类或java公共接口或xml资源文件。?什么时候?为什么?
- 哪个提供更好的性能?,什么时候?为什么?
Ex: xml resource
例如:xml 资源
<integer name="config_build_type">0</integer>
<string-array name="url_authentication">
<item >http://development.com/xxxx</item>
<item >http://test.com/xxx</item>
<item >http://example.com/xxx</item>
</string-array>
Java static constant
Java静态常量
public class Config {
public static final int BUILD_TYPE = 0; // 0 - development, 1 - test, 2 - live
public static final String[] URL_AUTHENTICATION = {"http://development.com/", "http://test.com/", "http://example.com"};
}
采纳答案by Alex Lockwood
There is a big difference between the two in that you can reference project resources in your XML layouts. They are available in the application context and are therefore accessible across the global application. The biggest advantages of using project resources is the ease of access and that they allow you to organize your project significantly.
两者之间的很大区别在于您可以在 XML 布局中引用项目资源。它们在应用程序上下文中可用,因此可以跨全局应用程序访问。使用项目资源的最大优势是易于访问,并且它们允许您显着地组织您的项目。
static final
constants are compiled into the java bytecode; project resources are compiled into a binary format within the apk. Accessing either is extremely efficient... if there is a difference between the two, it is trivial at most.
static final
常量被编译成java字节码;项目资源在apk内编译成二进制格式。访问任何一个都非常有效......如果两者之间存在差异,最多是微不足道的。
There isn't a set rule on how you should be using resources/constants in your project. That said, I personally use resources for values that I might need to use in my XML orjava code. On the other hand, I typically use static final
constants for values that will onlybe used by my java code and are specific to my implementation.
关于如何在项目中使用资源/常量并没有固定的规则。也就是说,我个人将资源用于可能需要在我的 XML或java 代码中使用的值。另一方面,我通常将static final
常量用于仅由我的 java 代码使用并且特定于我的实现的值。
Also note that it is possible to load XML resources at runtime depending on the device's current configuration (i.e. screen size, locale, etc.). So you should take this into consideration when deciding whether or not you should declare the constant in XML or directly in your .java
files.
另请注意,根据设备的当前配置(即屏幕大小、区域设置等),可以在运行时加载 XML 资源。因此,在决定是否应在 XML 中或直接在.java
文件中声明常量时,应考虑到这一点。
回答by Mahen
For the people who want to see how we can use a Class to define our constants and call any where we need.
对于想要了解我们如何使用 Class 来定义我们的常量并在我们需要的任何地方调用的人。
Constant.java
常量.java
package org.nrum.nrum;
/**
* Created by rajdhami on 5/23/2017.
*/
public class Constant {
public static final String SERVER = "http://192.168.0.100/bs.dev/nrum";
// public static final String SERVER = "http://192.168.100.2/bs.dev/nrum";
public static final String API_END = SERVER + "/dataProvider";
public static final String NEWS_API = API_END + "/newsApi";
public static final String BANNER_API = API_END + "/bannerApi/lists";
public static final String NOTICE_API = API_END + "/noticeApi/lists";
public static final String UPLOAD_PATH = SERVER + "/uploads";
public static final String UPLOAD_PATH_BANNER = UPLOAD_PATH + "/company_1/banner";
public static final String UPLOAD_PATH_NEWS = UPLOAD_PATH + "/company_1/news";
public static final int BANNER_TRANSITION_DURATION = 5000;
public static final int NOTICE_BUTTON_BLINK_DURATION = 5000;
public static final int BANNER_FETCH_LIMIT = 3;
}
Now we can use above constants in following way.
现在我们可以通过以下方式使用上述常量。
Constant.NOTICE_BUTTON_BLINK_DURATION
回答by Dheeresh Singh
In general case:
一般情况下:
- XML values have the advantage of accessbilty in layout file and manifest file over Constants in java file
- XML values have the advantage for multi language support over Constants in java file
- XML 值在布局文件和清单文件中比 java 文件中的常量具有可访问性的优势
- XML 值比 java 文件中的常量具有多语言支持的优势
回答by Samir Mangroliya
It's always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.
从应用程序代码中提取 UI 字符串并将它们保存在外部文件中始终是一个好习惯。Android 通过每个 Android 项目中的资源目录使这变得容易。
http://developer.android.com/training/basics/supporting-devices/languages.html
http://developer.android.com/training/basics/supporting-devices/languages.html
回答by Venky
I think both way seems to be good but thing is that it depends on your requirements.
我认为这两种方式似乎都不错,但问题是这取决于您的要求。
If you have your values(web service link) in your XML and suppose there is any change in your values(web service link) , you can easily change only in XML file.
如果您的 XML 中有您的值(网络服务链接)并且假设您的值(网络服务链接)有任何更改,则您可以轻松地仅在 XML 文件中进行更改。
But if you use inside classes as static variables you have to change in all class files.
但是,如果您将内部类用作静态变量,则必须在所有类文件中进行更改。
So my suggestion is that separate constants from source file and put into resource and access it..
所以我的建议是将常量与源文件分开并放入资源并访问它..
回答by PVS
I am glad someone asked this ... plus one!
我很高兴有人问这个......加一!
Project resources need access to Context, which is not available in static methods (unless you pass it in etc), but always available in Activity -- there seems to be a preferential connection between resources and layouts. For app variables and constants that may be processed in static methods I create an abstract class and do a static import (of this constants class) in all the other project class files.
项目资源需要访问 Context,这在静态方法中不可用(除非您将其传入等),但在 Activity 中始终可用 - 资源和布局之间似乎存在优先连接。对于可以在静态方法中处理的应用程序变量和常量,我创建了一个抽象类,并在所有其他项目类文件中执行(这个常量类的)静态导入。
PVS
PVS