使用getIdentifier()获取资源Id

Java代码  
  1. int i=  getResources().getIdentifier("icon", "drawable", getPackageName()) ;
  2. if(i>0)
  3. {Log.i("aa","aa");}
  4. else
  5. {Log.i("vbv","aa");}
int i=  getResources().getIdentifier("icon", "drawable", getPackageName()) ;
if(i>0)      {Log.i("aa","aa");}
else      {Log.i("vbv","aa");}

或者

Java代码  
  1. int resID = getResources().getIdentifier("org.loveandroid.androidtest:drawable/gallery_photo_1",null,null);
  2. int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
  3. // or
  4. int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
  5. //第一个参数:full_package:type/filename_without_ending是这种格式 然后其他的可以为null
int resID = getResources().getIdentifier("org.loveandroid.androidtest:drawable/gallery_photo_1",null,null);int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
// or
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");//第一个参数:full_package:type/filename_without_ending是这种格式 然后其他的可以为null
Java代码  
  1. int idFlag = getResources().getIdentifier(getPackageName() + ":drawable/flag", null, null);
  2. // 或是
  3. int idFlag = getResources().getIdentifier("flag", "drawable", getPackageName());
int idFlag = getResources().getIdentifier(getPackageName() + ":drawable/flag", null, null);
// 或是
int idFlag = getResources().getIdentifier("flag", "drawable", getPackageName());
Java代码  
  1. var Drawable[] dw = new Drawable[10];
  2. for (int i = 1; i <= 10; i++) {
  3. int id = getResources().getIdentifier("flag" + i,  "drawable", getPackageName());
  4. dw[i-1] = getResources().getDrawable(id);
  5. }
var Drawable[] dw = new Drawable[10];     for (int i = 1; i <= 10; i++) {     int id = getResources().getIdentifier("flag" + i,  "drawable", getPackageName());     dw[i-1] = getResources().getDrawable(id);
}
Java代码  
  1. //用反射法 可以得到 所有的资源
  2. private void
  3. _DumpAllResourceIDs(Class<?> classType)
  4. throws IllegalArgumentException {
  5. Field[] fIDs = classType.getFields();
  6. try {
  7. for (int i = 0; i < fIDs.length; i++) {
  8. Field fld = fIDs[i];
  9. int nID = fld.getInt(null);
  10. Log.d("dbg",
  11. classType.getSimpleName() + " " +
  12. i + ": " +
  13. fld.getName() + "=" +
  14. nID);
  15. }
  16. } catch (Exception e) {
  17. throw new IllegalArgumentException();
  18. }
  19. }
//用反射法 可以得到 所有的资源
private void      _DumpAllResourceIDs(Class<?> classType)      throws IllegalArgumentException {     Field[] fIDs = classType.getFields();     try {     for (int i = 0; i < fIDs.length; i++) {     Field fld = fIDs[i];     int nID = fld.getInt(null);     Log.d("dbg",     classType.getSimpleName() + " " +      i + ": " +      fld.getName() + "=" +     nID);     }     } catch (Exception e) {     throw new IllegalArgumentException();     }
}
Java代码  
  1. import java.lang.reflect.Field;
  2. ...
  3. _DumpAllResourceIDs(R.layout.class);
  4. _DumpAllResourceIDs(R.drawable.class);
import java.lang.reflect.Field;
...     _DumpAllResourceIDs(R.layout.class);     _DumpAllResourceIDs(R.drawable.class);
Java代码  
  1. //结果
  2. R$layout 0: main=2130903040
  3. R$layout 1: small_spinner_dropdown_item=2130903041
  4. R$drawable 0: icon=2130837504
//结果
R$layout 0: main=2130903040
R$layout 1: small_spinner_dropdown_item=2130903041
R$drawable 0: icon=2130837504

有时候我们需要动态的取得一个一个控件的id,然后进行操作,经过在网上查找,找到了一下方法

getResources().getIdentifier("textView01", "id", "cn.xxx.xxx");

第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。

做项目过程中遇到一个问题,从数据库里读取图片名称,然后调用图片。直接用R.drawable.?无法调用。查了好多地方最后找到了个方法,分享给大家,希望有帮助。
主要由两种方法,个人建议第二种。
1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:
String path = "com/drawable/resource/imageName.png";
InputStream is = getClassLoader().getResourceAsStream(path);
Drawable.createFromStream(is, "src");

2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:
假设创建工程的时候,填写的package名字为:com.test.image
int resID = getResources().getIdentifier("imageName", "drawable", "com.test.image");
Drawable image = getResources().getDrawable(resID);

getResources().getIdentifier(textView01, id, cn.xxx.xxx);相关推荐

  1. pom文件报红Failure to find cn.xx:xxx:pom:3.x.x in https://maven.aliyun.com/repository/public/ was cached

    背景:换了项目依赖的本地库的位置,重新下载了依赖. 这里这个project标签本来是报红的,错误信息是:Failure to find cn.xx:xxx:pom:3.x.x in https://m ...

  2. Python中的xxx+=xxx和xxx=xxx+xxx一些区别及执行过程

    预知小知识: Python中的变量与其他语言稍有差异,如a = 10并不是直接在内存中创建一个变量a其值为10,而是在内存中创建一个a这个a指向这个10,在Python中所有牵扯到等号的均不是值赋值, ...

  3. 报错error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.xxx.xxx:style/Theme)not found.

    报错内容如下: error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.xxx.xxx:style/Theme.AppCo ...

  4. [ERROR] Slave SQL for channel ‘‘: Could not execute Write_rows event on table xxx.xxx; Duplicate ent

    环境:centos7 , MySQL5.7一主一从 背景:从上用xtrabackup物理恢复后,感觉gtid相差不大,直接change master to master_host........ 问题 ...

  5. 1.出现了 page[pages/XXX/XXX] not found.May be caused by :1. Forgot to add page route in app.json.2. Inv

    以下是自己在开发过程中遇到的坑和小技巧,记录以下: 1.出现了 page[pages/XXX/XXX] not found.May be caused by :1. Forgot to add pag ...

  6. MySQL登录时出现 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES) 的原因及解决办法

    MySQL登录时出现 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES) 的原因及解决办法. # mysql - ...

  7. 解决使用Python连接Mysql数据库时出现错误:Access denied for user 'xxx'@'xxx' (using password: NO)

    事件背景 我今天在台式电脑上使用pymysql模块连接MySQL数据库时,运行py程序出现报错,报错提示为:Access denied for user 'xxx'@'xxx' (using pass ...

  8. 连接 MySQL 实例出现 Access denied for user ‘XXX’@’XXX’ 报错如何处理?

    现象描述 连接云数据库 MySQL 实例时,提示"ERROR 1045 (28000): Access denied for user 'XXX'@'XXX'"报错信息. 可能原因 ...

  9. springboot踩坑日记——Field xxx.xxx.xxx required a bean of type 'xxx.xxx.xxx' that could not be found

    使用feign时报创建的feign接口找不到 Field xxx.xxx.xxx required a bean of type 'xxx.xxx.xxx' that could not be fou ...

最新文章

  1. Python List 列表list()方法
  2. 法总统:英国若“无协议脱欧” 将成最大输家
  3. java calendar.add方法_Java Calendar add()方法与示例
  4. 【4】青龙面板系列教程之QQ通知机器人XDD-plus安装
  5. Unity性能优化-遮挡剔除
  6. IIS服务器Web访问提示输入密码问题
  7. 【mongodb系统学习之四】查看mongodb进程
  8. c语言动态存储分配和链表,C语言静态链表和动态链表
  9. asp.net中打印指定控件内容
  10. 使用LazZiya.ExpressLocalization开发多区域性ASP.NET Core 3、2、1项目
  11. matlab scope 怎么调出,Matlab Figure图形保存以及Simulink中Scope窗口调出figure菜单
  12. 51单片机入门——数码管
  13. mac 爱普生打印机驱动_爱普生l301打印机驱动Mac版-爱普生L301驱动Mac版下载 V9.31-PC6苹果网...
  14. flutter 集成融云 sdk
  15. “双态IT”成就业务“互联网+”转型 —— 联想发布《联想双态IT白皮书》和“双态IT”解决方案家族...
  16. MacBook上有哪些相见恨晚的神器
  17. [毒]QQ空间出现的伪装QQ登录窗口诈骗
  18. 微信开发工具button跳转页面_微信小程序按钮点击跳转页面详解
  19. linux 视频壁纸,Plasma video wallpaper: KDE 4 视频壁纸
  20. 四川大学信息服务平台 ScuInfo网页 JSON数据解析

热门文章

  1. 2018-2019-1 20165325 20165320 20165337 实验二 固件程序设计
  2. 【工具使用系列】关于 MATLAB 液压元件,你需要知道的事
  3. 7.08 计算累计差
  4. 使用spring + ActiveMQ 总结
  5. script中用php
  6. 网络硬盘与传统资源共享的不同
  7. WEB API的安全问题
  8. centos 升级4.0 内核
  9. Docker核心技术
  10. 设置mysql远程连接root权限