Android 获取有规律资源Id解决方案

简介: 在多个有规律的资源ID获取的时候,可以使用getIdentifier方法来获取,来获取。 用到场景:工具类打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源.

在多个有规律的资源ID获取的时候,可以使用getIdentifier方法来获取,来获取。

用到场景:工具类打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源.

public class Resources int getIdentifier (String name, String defType, String defPackage)
Return a resource identifier for the given resource name.
param defType:"layout","string","drawable","style","color","array"
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.)

from: http://developer.android.com/reference/android/content/res/Resources.html

  

public static int getDrawableId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, 
                "drawable", paramContext.getPackageName()); 
    } 
     

 

对于这个方法,官方不推荐:

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

 

更好的解决方案,是参照另外一篇博客的使用反射实现的。

http://www.liaohuqiu.net/cn/posts/android-get-resource-id-by-string/

示例代码:

public static int getResId(String variableName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

  

使用:

 

int id = ResourceMan.getResId("icon", R.drawable.class);

  

这种效率据说比第一种高了4倍。

 

目录
相关文章
|
XML Java Android开发
Android Studio开发APP启动程序时开屏简单动画效果快速有效解决方案
Android Studio开发APP启动程序时开屏简单动画效果快速有效解决方案
1325 0
Android Studio开发APP启动程序时开屏简单动画效果快速有效解决方案
|
1月前
|
JSON Android开发 数据格式
android 使用GSON 序列化对象出现字段被优化问题解决方案
android 使用GSON 序列化对象出现字段被优化问题解决方案
|
8月前
|
IDE Java 开发工具
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8的解决方案
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8的解决方案
|
8月前
|
Java Android开发
Android 保存资源图片到相册最新写法适用于Android10.0及以上
Android 保存资源图片到相册最新写法适用于Android10.0及以上
572 0
|
8月前
|
编解码 监控 前端开发
Android平台GB28181设备接入端如何降低资源占用和性能消耗
Android平台GB28181设备接入端如何降低资源占用和性能消耗?
|
7月前
|
Android开发
Android Studio 控制台中文乱码,解决方案都在这里了,完美解决
Android Studio 控制台中文乱码,解决方案都在这里了,完美解决
|
8月前
|
Android开发
Android 中ViewPager嵌套RecyclerView出现滑动冲突的解决方案
Android 中ViewPager嵌套RecyclerView出现滑动冲突的解决方案
696 0
|
8月前
|
Java Android开发
Android 中使用数组资源文件定义数组
Android 中使用数组资源文件定义数组
116 0
|
8月前
|
Android开发
Android > Project with path ‘:audiovisualize‘ could not be found in project ‘:app‘. 异常解决方案
Android > Project with path ‘:audiovisualize‘ could not be found in project ‘:app‘. 异常解决方案
41 0
|
9月前
|
网络协议 Linux Android开发
Android部分手机4G网第一次请求很慢(wifi正常)解决方案
Android部分手机4G网第一次请求很慢(wifi正常)解决方案
255 0