从另一个包中获取资源
在src/内部,我有以下软件包:com.app.animals,com.app.animals.threads,com.app.animals.games
Inside src/ I have the following packages:com.app.animals, com.app.animals.threads, com.app.animals.games
当我尝试从com.app.animals.games内名为"CarreraJuego.java"的类中获取Proyect资源(可绘制文件夹)时,它说"R无法解析为变量"
When I try to get my proyect resources (drawable folder) from a class called "CarreraJuego.java" inside com.app.animals.games, it says that 'R cannot be resolved to a variable'
如何从该程序包访问资源文件夹?该类是Surfaceview
How can I access resources folder from that package?The class is a surfaceview
这是一种简单而又快速的方法:
Here is a simple and quick way to achieve it:
try
{
PackageManager manager = getPackageManager();
Resources resources = manager.getResourcesForApplication("com.example.packagename");
int resId = resources.getIdentifier("drawable_id", "drawable", "com.example.packagename");
Drawable myDrawable = resources.getDrawable(resId);
// Do something
}
catch (Exception e)
{
e.printStackTrace();
}
更改com.example.packagename
宽度所需资源的应用程序的程序包名称.
用资源ID更改drawable_id
.
将drawable
更改为所需的文件夹名称(drawable
,layout
,values
,...)
Change com.example.packagename
width the package name of the app you need the resources.
Change drawable_id
with the resource Id.
Change drawable
to the folder name you want (drawable
, layout
, values
, ...)