我有一个字符串来标识我想要获取的资源。
我知道以下内容:
public int getIdentifier(String name, String defType, String defPackage)
但是我需要在外部类中使用它,但它不允许我这样做。
例如,我有一个名为“thefile”的字符串,我想访问 R.raw.thefile
有什么想法吗?我被困住了
干杯
最佳答案
您可以将 Context 传递给外部类。示例...
public Myclass {
public void doSomething (Context context) {
int resId = context.getResources().getIdentifier(...);
}
然后在您的 Activity 中...
MyClass myClass = new MyClass();
myClass.doSomething(this);
关于android - 从外部类中的名称获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161890/