我不知道那个
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; } }这样将用于获取
R.drawable.icon资源整数值的值
int resID = getResId("icon", R.drawable.class); // or other resource class我刚刚找到了一篇博客文章,说这
Resources.getIdentifier()比像我那样使用反射要慢



