Android 控件背景图片和imagebutton的使用(selector)

1、背景图片的使用

在使用xml做布局的时候使用andriod:background = "@drawable/your_image"
其中“your_image”就是你放在res/drawable/目录下的需要设置成的背景图片的图片,只需要图片名不要后缀名。

2、imagebutton的使用

在mainxml文件中定义imagebutton

      android:id="@+id/btnBack1" 
      android:layout_width="78sp" 
      android:layout_height="43sp"              
      android:src="@xml/querybuttonstate" 
      android:focusable="true"  
  />

android:src="@xml/querybuttonstate"是res/xml/querybuttonstate.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=http://schemas.android.com/apk/res/android"> 
<item 
android:drawable="@drawable/gaga" 
android:state_selected="false" 
android:state_pressed="false" /> 
<item 
android:drawable="@drawable/gaga_2" 
android:state_selected="false" 
android:state_pressed="true" /> 
<item 
android:drawable="@drawable/gaga_2" 
android:state_selected="true" />
</selector> 

这里要准备两张图片,代表两种状态。

可以参考这篇文章:http://www.javaeye.com/topic/575279

3.drawableTop

//update 2014.11.15

drawableTop是button(imagebuttion,radiobutton),TextView,imageview设置上部显示图片(drawable),
当然其他方位也可以设置:drawableLeft,drawableRight,drawableBottom。

我们在layout中可以这样配置:
android:drawableTop="@drawable/my_selector"

当需要动态更换图片时,参考如下两个方法:
setCompoundDrawables(left, top, right, bottom);
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
例如:

button.setCompoundDrawablesWithIntrinsicBounds(null,
        getResources().getDrawable(R.drawable.my_selector), null, null);
两者有些区别(参考网友):
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,所以才有The Drawables must already have had setBounds(Rect) called.意思是说使用之前必须使用Drawable.setBounds设置Drawable的长宽。
而setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得,所以才有The Drawables' bounds will be set to their intrinsic bounds.这句话之说!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值