Android drawable 反射取资源id

通过反射来获取资源id

try{
    String resName = "ic_launcher";
    int resId = R.drawable.class.getDeclaredField(resName).getInt(R.drawable.class);
    imageView.setImageResource(resId);
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}    

 

posted @ 2014-11-26 20:25  rfheh  阅读(751)  评论(0)    收藏  举报