Android - View.getResources()和View.getContext().getResources()的区别

时间:2020-12-27 04:10:22

I wanted to understand the real difference in using View.getResources() and View.getContext().getResources().

我想了解使用View.getResources()和View.getContext(). getresources()的真正区别。

For example, I have to set a color to a TextView from resource..

例如,我必须从资源中为TextView设置一个颜色。

view.setTextColor(view.getResources().getColor(R.color.Blue));

or

view.setTextColor(view.getContext().getResources().getColor(R.color.Blue));

Both works , but as per the documents...

这两个都可以,但是根据文件……

View.getResources() - Returns the resources associated with this view.

getresources() -返回与此视图关联的资源。

View.getContext() - Returns the context the view is running in, through which it can access the current theme, resources, etc.

getcontext()——返回视图正在运行的上下文,通过该上下文可以访问当前的主题、资源等。

Your thoughts are welcome....

你的想法是受欢迎的....

3 个解决方案

#1


15  

Nothing. As seen in the source code:

什么都没有。如源代码所示:

private final Resources mResources;

public View(Context context) {
    mContext = context;
    mResources = context != null ? context.getResources() : null;
    ...
}

public Resources getResources() {
    return mResources;
}

#2


1  

I would guess the View.getResources() is a shortcut for getContext().getResources().

我认为View.getResources()是getContext(). getresources()的快捷方式。

You could search the source code if you really want to know.

如果你真的想知道,你可以搜索源代码。

#3


0  

getContext().getResources() returns resources of the contest in which View is running i.e. this can be used when views are implemented dynamically while View.getResource(). Return a Resources instance for your application's package.

getContext(). getresources()返回视图正在运行的竞赛的资源,也就是说,当视图在View. getresource()中动态实现时,可以使用这个资源。返回应用程序包的资源实例。

#1


15  

Nothing. As seen in the source code:

什么都没有。如源代码所示:

private final Resources mResources;

public View(Context context) {
    mContext = context;
    mResources = context != null ? context.getResources() : null;
    ...
}

public Resources getResources() {
    return mResources;
}

#2


1  

I would guess the View.getResources() is a shortcut for getContext().getResources().

我认为View.getResources()是getContext(). getresources()的快捷方式。

You could search the source code if you really want to know.

如果你真的想知道,你可以搜索源代码。

#3


0  

getContext().getResources() returns resources of the contest in which View is running i.e. this can be used when views are implemented dynamically while View.getResource(). Return a Resources instance for your application's package.

getContext(). getresources()返回视图正在运行的竞赛的资源,也就是说,当视图在View. getresource()中动态实现时,可以使用这个资源。返回应用程序包的资源实例。