3 回答
TA贡献1797条经验 获得超6个赞
R.drawable.resourcename
Android.R
getResources
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
this.context
Activity
, Service
Context
最新情况:
Resources
getResources()
getResourceName(int)
getResourceTypeName(int)
?
更新2:
Resources
public int getIdentifier (String name, String defType, String defPackage)
TA贡献1802条经验 获得超5个赞
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
int resourceId = this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName());
TA贡献1851条经验 获得超4个赞
从字符串获取资源ID的简单方法。在这里,ResourceName是可绘制文件夹中资源ImageView的名称,它也包含在XML文件中。
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);
添加回答
举报