- 浏览: 497689 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (200)
- java基础 (30)
- ajax (19)
- 乱写 (5)
- groovy (2)
- db (8)
- gwt (0)
- jee (2)
- 我关注的开源 (1)
- RIA AIR (1)
- spring (11)
- lucene (0)
- 工具 (10)
- 百科 (2)
- linux (6)
- android (40)
- 移动开发 (21)
- 代码片断 (15)
- tomcat (1)
- css (1)
- html5 (2)
- jquery (2)
- playframework (3)
- web (2)
- nio (3)
- design (1)
- nosql (3)
- 日志 (12)
- mysql (4)
- 图表 (1)
- python (3)
- ruby (1)
- git (0)
- hibernate (1)
- springboot (1)
- guava (1)
- mybatis (0)
- 工作问题 (3)
- php (1)
最新评论
-
linzm1990:
踩了很多坑啊。。。。
hibernate @Nofound 与@ManyToOne fetch lazy的问题 -
Ccccrrrrrr:
...
转: Spring boot 文件上传 -
rmzdb:
兄弟,你这个东西,在ie内核的浏览器,貌似不识别 文件名
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
107x:
问题解决了,谢谢!
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
klxqljq:
额鹅鹅鹅
android布局实现头尾固定, 中间多余内容可以滚动
使用getIdentifier()获取资源Id
或者
有时候我们需要动态的取得一个一个控件的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);
int i= getResources().getIdentifier("icon", "drawable", getPackageName()) ; if(i>0) {Log.i("aa","aa");} else {Log.i("vbv","aa");}
或者
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
int idFlag = getResources().getIdentifier(getPackageName() + ":drawable/flag", null, null); // 或是 int idFlag = getResources().getIdentifier("flag", "drawable", getPackageName());
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); }
//用反射法 可以得到 所有的资源 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(); } }
import java.lang.reflect.Field; ... _DumpAllResourceIDs(R.layout.class); _DumpAllResourceIDs(R.drawable.class);
//结果 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);
发表评论
-
jackson知识点
2014-05-05 14:13 15831. jackson处理boolean类型的注意点 在使用j ... -
ChartDirectorvk如何测试文本的长度跟宽度
2012-11-30 15:53 1098在使用charDirector画图时, 要确定setPlotA ... -
android listview
2012-07-13 17:37 887ListView与Button的共存问题解决, 解决在list ... -
消息系统部署、维护文档 (HornetQ)
2012-03-06 08:31 0hornetq中文参考文档 一、服务器部署 目前消息服务部 ... -
netty与tomcat等nio的比较(取自zhh2009在论坛里的发言)
2012-03-05 23:58 6848源讨论: http://www.iteye.com/topic ... -
LinkedHashmap的构建函数的第三个参数引发的问题
2012-03-02 17:47 3923注意: 这里只有构造函 ... -
展示字符集编码表示
2012-03-02 13:43 1176import java.nio.ByteBuffer; ... -
nginx 预压缩(gzip)静态文件
2012-01-31 10:01 1643转自: http://willko.iteye.com/blo ... -
米聊所采用的一些技术
2012-01-31 09:52 2042nginx upstream fallback 设置 up ... -
演化理解 Android 异步加载图片
2011-11-09 09:55 859LinearLayout 布局,其下放了5个ImageView ... -
android常用颜色
2011-11-07 08:49 1235常用颜色值: 可以完美的颜色比对的网站: http://w ... -
dialog,activity 屏蔽Home键详解
2011-11-03 09:39 0http://www.iteye.com/topic/1116 ... -
android SlidingDrawer example
2011-11-03 09:35 0http://disanji.net/2010/12/16/a ... -
play flash swf file in android with webview
2011-11-03 09:34 0http://androidforums.com/applic ... -
AnimationDrawable 在Dialog中不能动画的原因(转)
2011-11-03 09:33 1262原来在dialog的onCreate onStart调用的时候 ... -
Free Android UI library & component roundup
2011-11-03 09:27 1111http://java.dzone.com/articles/ ... -
Android Fundamentals: Scheduling Recurring Tasks
2011-11-03 09:26 944http://mobile.tutsplus.com/tuto ... -
Android ListView pull up to refresh 改造(转)
2011-11-03 09:25 2059转自: http://dengyin2000.iteye.co ... -
Android中dp和px之间进行转换
2011-11-03 09:02 2217在xml布局文件中,我们既可以设置px,也可以设置dp(或者d ... -
view的setTag() 和 getTag()应用
2011-10-31 12:19 29880View中的setTag(Onbect)表示给View添加一个 ...
相关推荐
本文实例讲述了Android使用getIdentifier()获取资源Id的方法。分享给大家供大家参考,具体如下: int i= getResources().getIdentifier(icon, drawable, getPackageName()) ; if(i>0) {Log.i(aa,aa);} else {Log....
接触过Android开发的同学们都知道在Android中访问程序资源...在这种情况下,使用文件名来得到资源ID显得事半功倍。 通过调用Resources的getIdentifier可以很轻松地得到资源ID。 几个简单的示例: 代码如下: Resources
使用getIdentifier()方法根据资源名来获取资源id
通过资源名称,资源类型,程序包名获取资源的映射ID,通过对应的资源获取方法,获取该ID对应的资源对象 例如: 获取string资源: int id = context.getResources().getIdentifier(iconName, "string", context....
SqInject目录一,功能说明二,接收说明三,使用范例四,工程模块说明五,版本说明一,功能说明1,使用BindView将属性和id绑定(绑定采用的是getIdentifier的方式绑定的,非R.id) 2,BindId将id的值注入到属性中...
Android安卓-获取控件的id(标识符)-根据控件id获取对应的值或控件名字-动态获取R.string的值
一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: <”OK”>> 客户端请求成功 << / string>> //string资源中 名称为OK的 id 获取方式 int strId = getResources().getIdentifier(OK, ...
使用getIdentifier()方法可以方便的获各应用包下的指定资源ID。 方式一 int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null); 第一个参数格式是:包名 + : +资源文件夹名 +...
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } 3,...
{ int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); try { return getResources().getDimensionPixelSize(resourceId); } catch (Resources....
C# 获取硬件参数的实现方法 示例代码: private static string GetIdentifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ; System.Management.ManagementClass mc = new ...
要串行执行任务,请使用扩展 {@link SerialRunnable} 的可运行调用 {@link #execute(Runnable)}。 执行器将保证以串行顺序执行任何具有相同标识符的可运行程序。 所有其他未实现 {@link SerialRunnable} 的任务将以...
交互编程 - 实验室作业 -... getIdentifier(name, " drawable " , context . getPackageName()); } 然后在您的视图类中,您有一个要将图像传递给的组件(例如 ),然后执行以下操作: ImageView img = // You either