android通过资源名称去获得资源ID
2012-08-27 11:00
260 查看
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
public int getIdentifier (String name, String defType, String defPackage)
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
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.)
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 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
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.)
相关文章推荐
- 通过字段名称动态获取资源的id(将Android工程做成jar包和资源文件)
- android 通过资源名称去获得资源R id
- android 通过字符串名称 检索资源ID或String
- android中根据资源文件的名称获得资源文件的Id
- android 通过资源文件名称获取资源文件id
- Android通过图片名称获取资源id
- Android中通过资源名称获取对应的ID
- Android 通过名称获取资源ID
- Android之通过资源名称获取资源Id
- Android 通过名称获取资源ID
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- 通过文件名获得资源ID,主要是图片.
- 【Res id】通过图片名称获取资源图片
- Android 通过字符串来获取R下面资源的ID 值
- android通过反射获得资源数据
- 通过Android-sharedUserId访问不同apk的资源文件(资源和数据库)
- Android通过反射获取资源ID
- Android通过图片名字获得ID
- Android通过拼接名字获取drawable下的图片(根据图片名称获取在drawable中的ID)
- Android利用资源名称获取其ID