怎么通过文件名的String,找到对应的资源ID

public int getId(String str){

Class<R.drawable> cls = R.drawable.class;

int value = 0;
try {
value = cls.getDeclaredField(str).getInt(null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}

return value;

}

posted @ 2013-06-27 11:47  linsen@java  阅读(270)  评论(0编辑  收藏  举报