从另一个包中获取资源 [英] Getting Resources from another package

查看:73
本文介绍了从另一个包中获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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更改为所需的文件夹名称(drawablelayoutvalues,...)

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, ...)

这篇关于从另一个包中获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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