Android 获取有规律资源Id解决方案
在多个有规律的资源ID获取的时候,可以使用getIdentifier方法来获取,来获取。
用到场景:工具类打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源.
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倍。
Android 获取有规律资源Id解决方案的更多相关文章
- 【转】 android获取设备唯一标识完美解决方案
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ...
- android 获取设备唯一标识完美解决方案
/** * deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符 * * 渠道标志为: * 1,andriod(a) * * 识别符来源标志: * 1, wifi mac地址(w ...
- Android获取状态栏高度、标题栏高度、编辑区域高度
一.Activity界面的划分 简单说明一下(上图Activity采用默认Style,状态栏和标题栏都会显示):最大的草绿色区域是屏幕界面,红色次大区域我们称之为"应用程序界面区域" ...
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
转自:http://www.jianshu.com/p/89687f618837 原因分析 当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误 ...
- [Android学习笔记]使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...
- Android根据字符串型的资源名获取对应资源id
有时候我们想动态的根据一个字符串资源名获得到对应的资源id,如根据不同的图片名称获得对应的图片,此时就应该考虑如何实现. 大家知道,在编译Android工程时,系统会自动生成一个静态资源类R,里面根据 ...
- 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id
有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...
- Android获取所有应用的资源id和对应的uri
背景 在某些应用中,为了实现应用apk资源放入重复利用,或者使用反射得到本应用的资源,需要使用反射方式获得,但Resources类中也自带了这种获取方式,并且功能更加强大 你可以获取string,co ...
- Android 通过名称获取资源ID
当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源id,所有我们没有办法直接使用,只能通过名称来获取到资源id, package com.example.administrator.de ...
随机推荐
- C#关于事件的几个好例子
#region // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息 public class BoiledEventArgs : EventArgs { public r ...
- javascript——touch事件介绍与实例演示
分类: javascript2014-02-12 16:42 1742人阅读 评论(0) 收藏 举报 touch事件touchmovetouchstarttouchend 前言 诸如智能手机和平板 ...
- 生成HFile文件后倒入数据出现Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.filter.Filter
数据导入的时候出现: at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclar ...
- 深搜基础题目 杭电 HDU 1241
HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...
- 27 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- word2vec配置到使用
(1)首先下载word2vec,地址:https://code.google.com/p/word2vec/,可能下载的时候有问题,google上不去,那么可以从csdn上面下载. 解压后目录如下: ...
- Javascript 拖拽的一些简单的应用——逐行分析代码,让你轻松了解拖拽的原理
今天我们来看看如何让拖拽的物体不能拖出某个div之外和拖拽的吸附功能 上次讲到我们的拖拽是不可拖出可视区范围的,在这基础上我们加个父级的div,不让他拖出父级.原理和之前的一样,简单吧. <di ...
- mysqli连接数据库函数
<?php $mysqli=@new mysqli("localhost", "root", "123456", "xsph ...
- 在github搭建你的个人主页
一. 有没有遇到过在简历上要求写项目地址的经历.或者面试时面试官问你的项目在线地址是多少. 二. github 不但有代码托管的功能,还可以搭建在线演示项目,对于一个没有多少私房钱的穷屌丝,这个是不是 ...
- CI的扩展机制
CI的扩展机制 在熟悉了CI的源码之后,它的简单明了的代码风格很有趣,这篇文章看看在CI是如何实现扩展的. 扩展包 扩展是为了完成特定的功能,在CI中,扩展包的开发只能在application/lib ...