回答
這將是這樣的:
R.drawable.resourcename
確保你沒有在Android.R
命名空間導入,因爲它可以混淆的Eclipse(如果那是你使用的是什麼)。
如果不工作,你總是可以使用一個上下文的getResources
方法...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
凡this.context
被intialised爲Activity
,Service
或任何其他Context
子類。
更新:
如果這是你想要的名稱,Resources
類(由getResources()
返回)有getResourceName(int)
方法和getResourceTypeName(int)
?
更新2:
的Resources
類具有此方法:
public int getIdentifier (String name, String defType, String defPackage)
哪個返回指定的資源名稱的整數,式&包。
如果我的理解沒錯,這是你想要
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
在哪裏「這」是一種活動,寫只是爲了澄清什麼。
如果你想在strings.xml中的String或UI元素,替代「繪製」
int resourceId = this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName());
我警告你的標識,獲取標識的這種方式實在是太慢了,只用在需要的地方。
鏈接到官方文檔:Resources.getIdentifier(String name, String defType, String defPackage)
這在編寫測試以確保某些字符串存在的情況下非常有用 – 2015-05-01 22:14:59
我不認識男人,我在帶有時間戳的getIdentifier()之前和之後添加了一個日誌,它告訴我它在0 - 1 ms內執行!所以速度並不慢,速度非常快!我用它從資源中獲取圖像,並且完美地工作。在Nexus5x上測試。 – 2017-04-25 21:16:24
我會用我的方法來獲取資源ID建議你。與使用getIdentidier()方法相比,它效率更高,速度較慢。
下面的代碼:
/**
* @author Lonkly
* @param variableName - name of drawable, e.g R.drawable.<b>image</b>
* @param с - class of resource, e.g R.drawable.class or R.raw.class
* @return integer id of resource
*/
public static int getResId(String variableName, Class<?> с) {
Field field = null;
int resId = 0;
try {
field = с.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
}
它不適用於所有情況。例如,如果您有
另外你的方法實際上並不快。因爲Java類的序列化從來不會很快。 – ddmytrenko 2013-10-23 10:39:11
int resourceID =
this.getResources().getIdentifier("resource name", "resource type as mentioned in R.java",this.getPackageName());
如果你解釋你發佈的代碼會更好。 – 2012-10-21 01:11:07
一個簡單的方法來從字符串獲取資源ID。這裏的resourceName是XML文件中包含的drawable文件夾中的資源ImageView的名稱。
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable",
context.getPackageName());
im.setImageResource(id);
我發現this class對處理資源非常有幫助。它有一些定義的方法來處理夢詩,顏色,圖形內容和字符串,像這樣的:
public static String getString(Context context, String stringId) {
int sid = getStringId(context, stringId);
if (sid > 0) {
return context.getResources().getString(sid);
} else {
return "";
}
}
除了@lonkly解決方案
- 看到反射和場無障礙
- 不必要的變數
方法:
/**
* lookup a resource id by field name in static R.class
*
* @author - ceph3us
* @param variableName - name of drawable, e.g R.drawable.<b>image</b>
* @param с - class of resource, e.g R.drawable.class or R.raw.class
* @return integer id of resource
*/
public static int getResId(String variableName, Class<?> с)
throws android.content.res.Resources.NotFoundException {
try {
// lookup field in class
java.lang.reflect.Field field = с.getField(variableName);
// always set access when using reflections
// preventing IllegalAccessException
field.setAccessible(true);
// we can use here also Field.get() and do a cast
// receiver reference is null as it's static field
return field.getInt(null);
} catch (Exception e) {
// rethrow as not found ex
throw new Resources.NotFoundException(e.getMessage());
}
}
- 1. 如何獲取資源文件中所有資源的名稱
- 2. 按名稱獲取資源
- 3. Android:Howto獲取Android資源的資源ID
- 4. 如何從Azure資源GUID獲取資源名稱?
- 5. 如何獲取資源名稱?
- 6. 使用嵌套資源時獲取父資源的名稱
- 7. 如何獲取DB資源知道DB名稱?
- 8. 將音頻文件的資源名稱轉換爲資源ID
- 9. Simpy資源:獲取ID?
- 10. 從typedarray獲取資源ID
- 11. 從hWnd獲取(資源)ID
- 12. 具有指定標識或名稱的資源已存在 - Dcoumentdb
- 13. 資源$ NotFoundException:資源ID#0x7f08005e
- 14. 如何獲取用戶名到MVC3中的資源名稱?
- 15. 獲取資源編號的值時沒有已知的包裝
- 16. 從nl.siegmann.epublib獲取資源的位圖資源加載資源
- 17. 任何獲取現有AVAsset資源名稱的方法?
- 18. 具有重複資源名稱的名稱空間
- 19. 如果我知道它的名字,我如何獲取圖像的資源ID?
- 20. 從libGDX資源獲取android資源ID或URL
- 21. 如何從它的字符串名稱獲取資源的值
- 22. 的Android資源$ NotFoundException:資源ID#0x7f020052
- 23. 如何獲取圖像資源的名稱作爲字符串
- 24. 如何獲取列表中資源文件的名稱?
- 25. 如何獲取ItemProcessor中的當前資源名稱?
- 26. Android:如何從使用名稱的資源獲取字符串?
- 27. 如何通過Android中的名稱獲取圖像資源?
- 28. 如何獲取鏈接到耶拿資源的資源列表?
- 29. 如何獲取嵌入式資源的資源Uri?
- 30. 資源管理器如何獲取HAWQ中的資源?
可能重複的[如果我知道它的名字,我如何獲取圖像的資源ID?](http://stackoverflow.com/questions/3042961/how-can-i-get-the-resource-id-如果我知道它的名字) – ghoti 2012-12-07 18:55:57