按数组中的字符串名称访问资源字符串
我正在尝试使用字符串数组从XML获取资源,因为它当前处于循环中。
谁能告诉我如何做到这一点?
资源名称与数组字符串名称相同,只是为了清楚。
到目前为止,我尝试过:
mMainEngine.mContext.getString(R.string.class.getField(Modification.ModicationNames[Current]).getInt(null)),
我没有想念你
int id = getResources().getIdentifier("name_of_resource","string", getPackageName());
是你所需要的
例如,我正在访问Drawable。 您可以从名称获取id,如下所示
Resources res = context.getResources(); String strpckg = context.getPackageName(); int id = res.getIdentifier(iconName, "drawable", strpckg);
您还可以访问Drawable:
Drawable drawable = res.getDrawable(id);