1.常用方法public int getResId(String name,Context context){ Resources r = context.getResources(); int id = r.getIdentifier(name,"drawable","com.demo"); return id; } 2.使用反射(推荐,性能高)public class ResourceMan { public static int getResId(String
当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源id,所有我们没有办法直接使用,只能通过名称来获取到资源id, package com.example.administrator.demo; import android.content.Context; /** * Created by Administrator on 2017/8/27 0027. */ public class GetResourcesUtils{ /** * 获取资源文件的id * * @param co
package com.cp.utils; import android.content.Context; public class CPResourceUtil { public static int getLayoutId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "layout", paramContext.ge
res 中我们可能会放很多图片和音频视频等.它们放在 R.drawable, R.raw 下面. 有一种情况是,比如我有一个数据库保存项目中声音的一些信息.声音的 id 就很难保存.因为我们不能把 R.raw.sound1 在数据库里保存为 text .解决办法是,有一个方法: int id = getResources().getIdentifier("sound1","raw","com.example.test"); sound1 是声音
在多个有规律的资源ID获取的时候,可以使用getIdentifier方法来获取,来获取. 用到场景:工具类打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源. public class Resources int getIdentifier (String name, String defType, String defPackage) Return a resource identifier for the given resour