`

使用getIdentifier()获取资源Id

 
阅读更多
使用getIdentifier()获取资源Id
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);
分享到:
评论
1 楼 123hehe321 2014-04-10  
[flash=200,200][url][list]
[*]
[/list][/url][/flash]

相关推荐

    Android使用getIdentifier()获取资源Id的方法

    本文实例讲述了Android使用getIdentifier()获取资源Id的方法。分享给大家供大家参考,具体如下: int i= getResources().getIdentifier(icon, drawable, getPackageName()) ; if(i&gt;0) {Log.i(aa,aa);} else {Log....

    Android中实现根据资源名获取资源ID

    接触过Android开发的同学们都知道在Android中访问程序资源...在这种情况下,使用文件名来得到资源ID显得事半功倍。 通过调用Resources的getIdentifier可以很轻松地得到资源ID。 几个简单的示例: 代码如下: Resources

    getIdentifierSample.rar

    使用getIdentifier()方法根据资源名来获取资源id

    CustomResManage.java

    通过资源名称,资源类型,程序包名获取资源的映射ID,通过对应的资源获取方法,获取该ID对应的资源对象 例如: 获取string资源: int id = context.getResources().getIdentifier(iconName, "string", context....

    SqInject:基于getIdentifier实现的Android控件注入库

    SqInject目录一,功能说明二,接收说明三,使用范例四,工程模块说明五,版本说明一,功能说明1,使用BindView将属性和id绑定(绑定采用的是getIdentifier的方式绑定的,非R.id) 2,BindId将id的值注入到属性中...

    yfz_getIdentifier.zip

    Android安卓-获取控件的id(标识符)-根据控件id获取对应的值或控件名字-动态获取R.string的值

    基于Android中获取资源的id和url方法总结

    一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: &lt;”OK”&gt;&gt; 客户端请求成功 &lt;&lt; / string&gt;&gt; //string资源中 名称为OK的 id 获取方式 int strId = getResources().getIdentifier(OK, ...

    android 获取APP的唯一标识applicationId的实例

    使用getIdentifier()方法可以方便的获各应用包下的指定资源ID。 方式一 int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null); 第一个参数格式是:包名 + : +资源文件夹名 +...

    android电子签名,屏幕上手写签名

    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId &gt; 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } 3,...

    swipeback-Android侧滑返回分析和实现(不高仿微信).zip

    { int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); try { return getResources().getDimensionPixelSize(resourceId);  } catch (Resources....

    C# 获取硬件参数的实现方法

    C# 获取硬件参数的实现方法 示例代码: private static string GetIdentifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ; System.Management.ManagementClass mc = new ...

    SerialAndParallelExecutor

    要串行执行任务,请使用扩展 {@link SerialRunnable} 的可运行调用 {@link #execute(Runnable)}。 执行器将保证以串行顺序执行任何具有相同标识符的可运行程序。 所有其他未实现 {@link SerialRunnable} 的任务将以...

    dinnerplanner-android-NTH_YS

    交互编程 - 实验室作业 -... getIdentifier(name, " drawable " , context . getPackageName()); } 然后在您的视图类中,您有一个要将图像传递给的组件(例如 ),然后执行以下操作: ImageView img = // You either

Global site tag (gtag.js) - Google Analytics