getIdentifier(String name, String defType, String defPackage )
name:表示资源文件的名称(不带后缀名),不可以为空,
defType:资源文件所在的文件夹:drawable,mipmap…
defPackage:包名,context.getPackageName()获取
适用于:网络请求的数据通过gson解析成对象或者直接用数据 获取某个文件夹下同名文件
如下展示通过网络请求后的数据解析成对象后 通过其中某一变量设置UI图片(图片在mipmap文件夹下):
int weaImageId = context.getResources().getIdentifier(hours.getWea_img(),"mipmap",context.getPackageName());
Glide.with(context)
.load(weaImageId)
.into(mTvWea);
本文介绍如何通过Context的getResources().getIdentifier方法,结合Glide库,从网络请求的数据中解析出资源ID,特别是针对mipmap文件夹下的图片资源,用于设置UI。示例代码展示了如何将网络请求的天气数据解析后,动态设置天气图片。
3万+

被折叠的 条评论
为什么被折叠?



