动态引用图片

在资源文件中存放有 image_1.png, image_2.png, image_3.png 三张图片 ,  根据传入参数动态引用对应的图片 , 有三个解决方法

根据R.drawable.xx动态引用是错误的 , 因为每个这种id都对应着R文件中的一个id,如果没有相对应的id , 编译不会通过;

建立一个工程,包名为com.yun.demo

方案一 : 图片放在drawable目录下的情况

[java] view plaincopy

  1. Resources resources = this.getResources();  
  2. int imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");  

使用上面的代码可以通过字符串拼接图片名称 , 根据传入的参数 , 拼接imageName字符串 , 从而动态的获取图片对应的id;

[java] view plaincopy

  1. resources.getIdentifier(imageName, "drawable","chao.yun.demo");  

这个方法返回的是图片对应的id ;

第一个参数是图片的名称 , 如果没有找到 , 返回0 ;

第二个参数是默认的资源类型 , 如果找的是图片 , 就是 "drawable" , 这个不是具体的目录 , 因此不用注明"drawable-hdpi"

第三个参数是包名 , 这个包名是创建工程时候的包名 , 是总的包名 , 与manifest配置文件中的包名相同;
详细代码 :

layout中的代码 :

 

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <LinearLayout   
  8.         android:id="@+id/ll_1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="0px"  
  11.         android:layout_weight="1"></LinearLayout>  
  12.       
  13.     <LinearLayout   
  14.         android:id="@+id/ll_2"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="0px"  
  17.         android:layout_weight="1"></LinearLayout>  
  18.       
  19.     <LinearLayout   
  20.         android:id="@+id/ll_3"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="0px"  
  23.         android:layout_weight="1"></LinearLayout>  
  24.   
  25. </LinearLayout>  

activity代码 :

[java] view plaincopy

  1. public class MainActivity extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.main);  
  6.           
  7.         LinearLayout ll_1 = (LinearLayout) findViewById(R.id.ll_1);  
  8.         LinearLayout ll_2 = (LinearLayout) findViewById(R.id.ll_2);  
  9.         LinearLayout ll_3 = (LinearLayout) findViewById(R.id.ll_3);  
  10.           
  11.         Resources resources = this.getResources();  
  12.   
  13.     String imageName = "image_" + 1;  
  14.     int imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");  
  15.     ll_1.setBackgroundResource(imageIndentify);  
  16.   
  17.     imageName = "image_" + 2;  
  18.     imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");  
  19.     ll_2.setBackgroundResource(imageIndentify);  
  20.   
  21.     imageName = "image_" + 3;  
  22.     imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");  
  23.     ll_3.setBackgroundResource(imageIndentify);  
  24.     }  
  25. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值