android 通过资源名称去获得资源R id
2011-12-05 16:25
387 查看
Resources resources = context.getResources();
int indentify = resources.getIdentifier(context.getPackageName()+":drawable/"+iconName, null, null);
if(indentify>0){
icon = resources.getDrawable(indentify);
}
以下是getIdentifier的API文档,可见最重要的是第一个参数,格式是:包名 + : + 资源文件夹名 + / +资源名
如果找到了,返回资源Id,如果找不到,返回0
Since: API Level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters
Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
int indentify = resources.getIdentifier(context.getPackageName()+":drawable/"+iconName, null, null);
if(indentify>0){
icon = resources.getDrawable(indentify);
}
以下是getIdentifier的API文档,可见最重要的是第一个参数,格式是:包名 + : + 资源文件夹名 + / +资源名
如果找到了,返回资源Id,如果找不到,返回0
public int getIdentifier (String name, String defType, String defPackage)
Since: API Level 1Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters
name | The name of the desired resource. |
---|---|
defType | Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type. |
defPackage | Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. |
Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
相关文章推荐
- android通过资源名称去获得资源ID
- Android 通过名称获取资源ID
- android开发 通过class.getResourceAsStream()方法 获得资源。
- Android 通过名称获取资源ID
- Android之通过资源名称获取资源Id
- Android通过图片名称获取资源id
- android 通过占位符来动态获得字符串资源
- Android中通过资源名称获取对应的ID
- 通过字段名称动态获取资源的id(将Android工程做成jar包和资源文件)
- android中根据资源文件的名称获得资源文件的Id
- android通过gradle切换域名,替换app的String资源(修改app名称,图标)
- android通过反射获得资源数据
- android 通过字符串名称 检索资源ID或String
- android 通过资源文件名称获取资源文件id
- Android 视频开发中如何通过url或者本地视频文件路径获得视频的第一帧图片
- Android通过BaseActivity获取到当前启动的Activity名称
- Android 资源名称(name)和id互相转换,动态获取
- Android 通过包名获取AndroidManifest.xml,res,assets下的资源
- android中通过ndk打印函数名称和行数
- Android 通过字符串来获取R下面资源的ID 值