2010-08-13 137 views
130

我想通過它的名字而不是它的int id來訪問像String或Drawable這樣的資源。如何獲取具有已知資源名稱的資源ID?

我將使用哪種方法?

+1

可能重複的[如果我知道它的名字,我如何獲取圖像的資源ID?](http://stackoverflow.com/questions/3042961/how-can-i-get-the-resource-id-如果我知道它的名字) – ghoti 2012-12-07 18:55:57

回答

116

這將是這樣的:

R.drawable.resourcename

確保你沒有在Android.R命名空間導入,因爲它可以混淆的Eclipse(如果那是你使用的是什麼)。

如果不工作,你總是可以使用一個上下文的getResources方法...

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource); 

this.context被intialised爲ActivityService或任何其他Context子類。

更新:

如果這是你想要的名稱,Resources類(由getResources()返回)有getResourceName(int)方法和getResourceTypeName(int)

更新2

Resources類具有此方法:

public int getIdentifier (String name, String defType, String defPackage) 

哪個返回指定的資源名稱的整數,式&包。

+0

Thankq爲您的答覆.R.drawable.resourcename我現在使用我需要通過傳遞資源名稱獲得其整數值 – Aswan 2010-08-13 11:45:03

+1

'R.drawable.resourcename' *是*整數。 – Rabid 2010-08-13 11:46:51

+0

嗨Rabid.What你說那是通過傳遞資源通過訪問R.drawable。資源值的任何方式 – Aswan 2010-08-13 11:47:23

268

如果我的理解沒錯,這是你想要

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)

+2

這在編寫測試以確保某些字符串存在的情況下非常有用 – 2015-05-01 22:14:59

+0

我不認識男人,我在帶有時間戳的getIdentifier()之前和之後添加了一個日誌,它告訴我它在0 - 1 ms內執行!所以速度並不慢,速度非常快!我用它從資源中獲取圖像,並且完美地工作。在Nexus5x上測試。 – 2017-04-25 21:16:24

6

我會用我的方法來獲取資源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; 

} 
+0

它不適用於所有情況。例如,如果您有 content比R.string類將有一個string_name字段。而你的方法在這一點上是行不通的。 – ddmytrenko 2013-10-22 14:32:11

+2

另外你的方法實際上並不快。因爲Java類的序列化從來不會很快。 – ddmytrenko 2013-10-23 10:39:11

22
int resourceID = 
    this.getResources().getIdentifier("resource name", "resource type as mentioned in R.java",this.getPackageName()); 
+4

如果你解釋你發佈的代碼會更好。 – 2012-10-21 01:11:07

9

一個簡單的方法來從字符串獲取資源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); 
0

我發現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 ""; 
    } 
} 
0

除了@lonkly解決方案

  1. 看到反射和場無障礙
  2. 不必要的變數

方法:

/** 
* 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()); 
    } 
} 
相關問題