getResources(),或创建自己的静态类? [英] getResources() or create own static class?

查看:139
本文介绍了getResources(),或创建自己的静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常打字:

someVar = getResources().getString(R.string.someString);

我有我解析和建设,以确保该文件保持一致几个XML文件,我已经把标记名称在res /价值/ strings.xml档案。同样的手柄在几个活动中,并且其中一些活动扩展ListActivity而其他人不这样创建一个简单的超类里面有这些变量(例如:

I have several XML files that I am parsing and building and in order to make sure that the files stay consistent, I have placed the tag names in the res/values/strings.xml file. The same handles are used throughout several activities, and some of those activities extend ListActivity while others do not so creating a simple super class which houses these variables ( ex:

public class thisClass extends thatClass 
{...}

public class thatClass 
{
    package String someTag = "this";
    package String otherTag = "that"; 
}

我将承担所有的)这些调用getResources(中可以得到pretty征税,并想知道,如果它是有益的,而不是建立一个R-类型的文件,我可以存储这些类型的常用变量静态恩:

I would assume that all of these calls to getResources() could get pretty taxing and was wondering if it is beneficial to instead create an R-type file where I can store these types of commonly used variables statically ex:

public final class globalVars 
{
    public static final class XML_TAGS 
    {
        static final String someTag = "this";
        static final String otherTag = "that";
    }
}

和引用这些变量像这样的:

and to reference these variables like such:

serializer.startTag("", globalVars.XML_TAGS.someTag);

而不是

serializer.startTag("", getResources().getString(R.string.someTag));

感谢您的输入!

推荐答案

OK,寻找到的<一个源$ C ​​$ C后href=\"http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/content/res/Resources.java.htm\"相对=nofollow> android.content.res.resources 和一些其他类,很明显,资源利用和获取资源,通过 getResources()是< STRONG>昂贵的相比静态类

OK, after looking into the source code of android.content.res.resources and some other classes, it is evident that using Resources and getting resources through getResources() is costly compared to a static class.

事实上,返回资源的实例是不是一个静态的容器,而是通过执行几个语句(无论是字符串或绘制或任何其他获得资源的对象形式)。

Indeed, the instance of Resources returned is not a static container but rather an object that gets resources by executing a couple of statements (whether a string or drawable or any other form).

然而 使用getResources()有它的好处


  • 它可以帮助你的您外部化资源

  • 对于任何类型的资源,您可以指定默认情况下并根据可能多个替代资源的区域设置屏幕深度/分辨率 ...

  • It helps you externalize your resources.
  • For any type of resource, you can specify default and multiple alternative resources depending maybe on Locale, Screen Depth/Resolution...

A 静态容器可能会提供一个成本更低的替代比使用资源,但请记住:在任何本地化的尝试之后会相对非常昂贵

A static container might provide a less costly alternative than using resources but remember: any later attempt at localization would be relatively extremely costly.

这篇关于getResources(),或创建自己的静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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