从字符串设置背景可绘制资源 [英] Set Background Drawable Resource from String
本文介绍了从字符串设置背景可绘制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我到处都看过,但是找不到通过字符串值以编程方式设置背景资源的示例吗?
I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?
例如:
Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");
这是可能的还是我必须作为一个大的switch语句来做?
Is this possible or would I have to do it as a big switch statement?
推荐答案
您为此目的具有 getResources().getIdentifier
.它从名称中返回资源的ID.
you have getResources().getIdentifier
for this purpose. It returns the id of the resources from its name.
例如:
int resId = getResources().getIdentifier("b", "drawable", getPackageName());