阅读更多

 

http://blog.csdn.net/jrcisme/article/details/6444653

http://hi.baidu.com/scusong2010/item/1b49b4d146eccce6b2f777e8

 

关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>   
 
  
      
  
          android:drawable="@drawable/pic1" />   
  
          android:drawable= "@drawable/pic2" />  
  
          android:drawable="@drawable/pic3" />   
  
          android:drawable="@drawable/pic4" />   
  
          android:drawable="@drawable/pic5" />   

使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg
或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);  
       ListView.setSelector(drawable);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:
android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:

android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下就是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>

   
   
   
   

Button还可以实现更复杂的效果,例如渐变啊等等。
drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
         / 
        //定义当button 处于pressed 状态时的形态。 
               
                      
                       
                         
                                                       android:bottom="10dp" android:right="10dp"/>  
                 
 
        
 
        //定义当button获得 focus时的形态 
                  
                        
                         
                            
                                                           android:bottom="10dp" android:right="10dp"/>                   
               
 
            

 
     最后,需要在包含 button的xml文件里添加两项。假如是 main.xml 文件,
我们需要在