public int getResource(String imageName){
Context ctx=getBaseContext();
int resId = getResources().getIdentifier(imageName, "drawable" , ctx.getPackageName());
return resId
}
获取的是drawable文件夹下的图片,而且图片文件名不要带后缀。比如想要获取drawable文件夹下的home.jpg,只需调用getResource("home") 即可.
转载于:https://www.cnblogs.com/jasonkent27/p/4178771.html
相关资源:Android依据名字通过反射获取在drawable中的图片