android 设置背景图片 xml的background和java的getDrawable()

转自:http://blog.csdn.net/zhy_cheng/article/details/7515518


教你如何设置背景图片

xml文件设置背景图片中:

  任意一个控件,button imageView 或layout,在其的xml属性设置中,添加

[java] view plaincopy
  1. android:background="@drawable/bg"    

即可实现设置其背景图片为bg. 

其中bg.bnp图片存放在drawable目录下。

drawable目录下存放大小图标共用的图片。drawable-hdpi中存放240 WVGA800 的模拟器或板子用的图片。drawable-mdpi存放对应的小图片

 

[java] view plaincopy
  1. <ImageView  
  2.                     android:layout_width="wrap_content"                    
  3.                     android:layout_height="wrap_content"                              
  4.                     android:src="@drawable/pic"/>   

  

同样,用src属性,也可设置imageView的图片为pic.png

 


 

java代码中设置或更改一个控件的背景图片:

 

[java] view plaincopy
  1. Resources resources = getContext().getResources();   
  2. Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);  
  3. layout.setBackgroundDrawable(btnDrawable);   

 

程序中,动态修改layout的背景图片,用setBackgroundDrawable()函数实现。设置其背景图片为 layout_bg.png

 

修改imageView1的背景图片为imageView2的背景图片:

imageView2.getDrawable()可获得图片2的背景。值为Drawable类型

imageView1.setImageDrawable(drawable);   设置imageView1的背景图片

 

即:

[java] view plain copy
  1. imageView1.setImageDrawable(imageView2.getDrawable());
  2.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值