Android,从string获取资源ID?

我需要传递一个资源ID到我的一个类中的一个方法。 它需要使用引用指向的id,也需要string。 我该如何最好地实现这一目标?

例如:

R.drawable.icon 

我需要得到这个的整数ID,但我也需要访问string“图标”。

如果所有我必须传递给方法是“图标”string,将是可取的。

@EboMike:我不知道Resources.getIdentifier()存在。

在我的项目中,我使用下面的代码来做到这一点:

 public static int getResId(String resName, Class<?> c) { try { Field idField = c.getDeclaredField(resName); return idField.getInt(idField); } catch (Exception e) { e.printStackTrace(); return -1; } } 

它会像这样使用:

 getResId("icon", Drawable.class); 

我刚刚发现一篇博客文章,说Resources.getIdentifier()比使用像我这样的reflection慢。 检查出来 。

你可以使用这个函数来获取资源ID。

 public static int getResourceId(String pVariableName, String pResourcename, String pPackageName) { try { return getResources().getIdentifier(pVariableName, pResourcename, pPackageName); } catch (Exception e) { e.printStackTrace(); return -1; } } 

所以如果你想得到像这样的可绘制的调用函数

 getResourceId("myIcon", "drawable", getPackageName()); 

而对于string,你可以这样称呼它

 getResourceId("myAppName", "string", getPackageName()); 

读这个

这是基于@Macarse的答案。

使用这个以更快和代码友好的方式获取资源Id。

 public static int getId(String resourceName, Class<?> c) { try { Field idField = c.getDeclaredField(resourceName); return idField.getInt(idField); } catch (Exception e) { throw new RuntimeException("No resource ID found for: " + resourceName + " / " + c, e); } } 

例:

 getId("icon", R.drawable.class); 

如何从资源名称中获取应用程序资源ID是一个相当常见和很好回答的问题。

如何从资源名称获取原生Android资源ID的回答不太好。 这里是我的解决scheme,通过资源名称获取Android可绘制资源:

 public static Drawable getAndroidDrawable(String pDrawableName){ int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", "android"); if(resourceId==0){ return null; } else { return Resources.getSystem().getDrawable(resourceId); } } 

该方法可以修改为访问其他types的资源。

如果你需要配对一个string和一个int,那么一个Map怎么样?

 static Map<String, Integer> icons = new HashMap<String, Integer>(); static { icons.add("icon1", R.drawable.icon); icons.add("icon2", R.drawable.othericon); icons.add("someicon", R.drawable.whatever); } 

你可以使用Resources.getIdentifier() ,尽pipe你需要在你的XML文件中使用你的string的格式,例如package:drawable/icon

我这样做,这是为我工作:

  imageView.setImageResource(context.getResources(). getIdentifier("drawable/apple", null, context.getPackageName())); 

从string获取资源ID的简单方法。 在这里, resourceName是在XML文件中包含的可绘制文件夹中的资源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); 

既然你说过你只想传递一个参数,而哪个参数似乎并不重要,你可以传入资源标识符,然后找出它的string名称,这样:

 String name = getResources().getResourceEntryName(id); 

这可能是获得这两个值的最有效的方式。 你不必乱七八糟地find一个更长的string中的“图标”部分。

在MonoDroid / Xamarin.Android中你可以这样做:

  var resourceId = Resources.GetIdentifier("icon", "drawable", PackageName); 

但是由于GetIdentifier并不推荐在Android中使用 – 你可以像这样使用Reflection:

  var resourceId = (int)typeof(Resource.Drawable).GetField("icon").GetValue(null); 

在那里我build议放一个try / catch或validation你传递的string。

从string资源名称获取Drawable id我正在使用此代码:

 private int getResId(String resName) { int defId = -1; try { Field f = R.drawable.class.getDeclaredField(resName); Field def = R.drawable.class.getDeclaredField("transparent_flag"); defId = def.getInt(null); return f.getInt(null); } catch (NoSuchFieldException | IllegalAccessException e) { return defId; } } 
 public int getDrawableName(Context ctx,String str){ return ctx.getResources().getIdentifier(str,"drawable",ctx.getPackageName()); }