何时以及为什么应该使用 getResources()?

发布于 2024-12-10 06:13:17 字数 1075 浏览 0 评论 0原文

我刚刚开始使用 Android 开发并尝试。

getResources() 的文档说它将[r]返回应用程序包的 Resources 实例。

在代码示例中,我已经看到它用于访问res中的资源,但似乎您可以直接访问它们。

例如,要从 res/values/strings.xml 检索 my_string

<!-- strings.xml -->
...
<string name="my_string">This is my string</string>
...

您可以使用这两种方式

第一种方式:

// MyActivity.java //  
... 
// No need to use getResources()
String msg = getString(R.string.my_string);
...

第二种方式:

// MyActivity.java //
...
// What is the point of getResources() here?
// It works but it requires "import android.content.res.Resources;" and is longer
Resources the_resources = this.getResources();
String msg = the_resources.getString(R.string.my_string);
...

那么,您什么时候会使用需要使用getResources()来访问资源?似乎没有必要,或者它是被隐式调用的,还是必须调用它来访问某些其他类型的资源或通过其他方式访问资源?

I'm just getting started with Android dev and playing around.

The documentation for getResources() says that it will [r]eturn a Resources instance for your application's package.

In code examples I've seen this used to access the resources in res, but it seems like you can just access them directly.

For example to retrieve my_string from res/values/strings.xml:

<!-- strings.xml -->
...
<string name="my_string">This is my string</string>
...

You can use these two ways

The first way:

// MyActivity.java //  
... 
// No need to use getResources()
String msg = getString(R.string.my_string);
...

The second way:

// MyActivity.java //
...
// What is the point of getResources() here?
// It works but it requires "import android.content.res.Resources;" and is longer
Resources the_resources = this.getResources();
String msg = the_resources.getString(R.string.my_string);
...

So, when would you be required to use getResources() to access a resource? Seems like it is not necessary, or is it being call implicitly, or must it be called to access certain other types of resources or to access resources in other ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回心转意 2024-12-17 06:13:18

资源有许多我们可能需要的辅助方法。

R.id、R.drawable 都返回 android 在构建时分配的动态 int。假设我们有一个要求,需要根据名称访问国家标志图像。

如果我们的图像名称为 us.png 并且我们的值是“us”。处理它的两种方法是

if(countryName.equals("us")){
    imageview.setImageRsource(R.drawable.us);
}

OR

Resources res = getResources();
int imageId = res.getIdentifier(getIdentifier(countryName, "drawable"
        ,"com.myapp");
imageview.setImageRsource(imageId);

第二种方法将是要走的方法,特别是当有超过 50 个国家/地区时,否则您最终会得到一个很长的 if-else 或 switch 语句。

当需要访问Assets文件夹中的内容时,也会使用资源对象

res.getAssets().open(YOUR FILE);

。资源实例也可以传递给其他类文件来访问资源。这些是您可以使用它的一些场景。

Resources have many helper methods that we may require.

R.id, R.drawable all return dynamic int assigned by android during build time. Suppose we have a requirement where we require to access a coutries flag image based on its name.

If we have image name as us.png and the value we have is 'us'. The 2 ways to handle it are

if(countryName.equals("us")){
    imageview.setImageRsource(R.drawable.us);
}

OR

Resources res = getResources();
int imageId = res.getIdentifier(getIdentifier(countryName, "drawable"
        ,"com.myapp");
imageview.setImageRsource(imageId);

The second method will be the way to go especially when there are more than 50 countries or you will end up with a very long if-else or switch statement.

The resources object is also used when you need to access the contents in the Assets folder

res.getAssets().open(YOUR FILE);

Resource instance can also be passed to other class files to access the the resources. These are some of the scenarios that you can use it for.

雨后咖啡店 2024-12-17 06:13:18

它将在 non_activity 类中很有用。

您将把上下文传递给类,然后您可以从中访问资源。

It will be useful in non_activity classes.

You will pass the context to the class and from that you can access the resources.

冰之心 2024-12-17 06:13:18

继承Activity的类也会继承getResources()。如果您有一个 Activity 类(或其派生类),您可以访问此方法(通过继承,这是面向对象编程中的一个非常非常基本的概念。如果您有一个类 < em>不是这样一个类的派生,您需要授予对这样一个上下文的访问权限才能访问所述资源,

如果您需要有关面向对象编程的继承、多态性等的更多信息,我。建议参考涉及此类问题的各种网站或学校课程 问题。

A class that inherits Activity also inherits getResources(). If you have a class that is an Activity (or a derivative thereof) you have access to this method (via inheritance, a really, really basic concept in object oriented programming. If you have a class that isn't a derivative of such a class, you need to grant access to such a context for access to said resources.

If you need more information about inheritance, polymorphism, or etc. regarding object oriented programming, I suggest referring to various websites or school curricula touching upon such issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文