关于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" ?>   
 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <!-- 默认时的背景图片 -->  
   <item android:drawable="@drawable/pic1" />    
 <!-- 没有焦点时的背景图片 -->  
   <item android:state_window_focused="false"   
         android:drawable="@drawable/pic1" />   
 <!-- 非触摸模式下获得焦点并单击时的背景图片 -->  
   <item android:state_focused="true" android:state_pressed="true"   
         android:drawable= "@drawable/pic2" />  
 <!-- 触摸模式下单击时的背景图片 -->  
   <item android:state_focused="false" android:state_pressed="true"   
         android:drawable="@drawable/pic3" />   
 <!--选中时的图片背景  -->  
   <item android:state_selected="true"   
         android:drawable="@drawable/pic4" />   
 <!--获得焦点时的图片背景  -->  
   <item android:state_focused="true"   
         android:drawable="@drawable/pic5" />   
 </selector>


使用些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"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_selected="true" android:color="#FFF" />
     <item android:state_focused="true" android:color="#FFF" />
     <item android:state_pressed="true" android:color="#FFF" />
     <item android:color="#000" />
 </selector>
 Button还可以实现更复杂的效果,例如渐变啊等等。
drawable/button_color.xml
 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">         / 
         <item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。 
               <shape> 
                      <gradient  android:startColor="#8600ff" /> 
                       <stroke   android:width="2dp" android:color="#000000" /> 
                        <corners android:radius="5dp" />  
                        <padding android:left="10dp" android:top="10dp" 
                                 android:bottom="10dp" android:right="10dp"/>  
                  </shape> 
         </item> 
         <item android:state_focused="true">//定义当button获得 focus时的形态 
                  <shape> 
                        <gradient android:startColor="#eac100"/> 
                         <stroke android:width="2dp" android:color="#333333"  color="#ffffff"/>
                          <corners android:radius="8dp" />   
                          <padding android:left="10dp" android:top="10dp" 
                                   android:bottom="10dp" android:right="10dp"/>                  
                 </shape> 
             </item>
 </selector>


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

android:focusable="true" 
      android:backgroud="@drawable/button_color"


这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。




android背景选择器selector使用方法


方法一:代码实现



1. 自定义状态效果可以通过代码实现,也可以通过xml定义style实现。



2. 下面先介绍代码实现,通过StateListDrawable定义Button背景。


3. 由于View类中PRESSED_ENABLED_STATE_SET值不是公共常量,所以通过继承来访问了。



特注:其他控件的效果,比如ImageView,也可以通过这种方法实现,但是由于ImageView默认是没焦点,不可点击的,需要自己更改(需要点击就设置android:clickable="true" , 需要能够选中就设置android:focusable="true" )。

java 代码:


package com.test.TestButton;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TestButton extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Integer[] mButtonState = { R.drawable.defaultbutton,
                R.drawable.focusedpressed, R.drawable.pressed };
        Button mButton = (Button) findViewById(R.id.button);
        MyButton myButton = new MyButton(this);
        mButton.setBackgroundDrawable(myButton.setbg(mButtonState));
    }

    class MyButton extends View {

        public MyButton(Context context) {
            super(context);
        }

        // 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选
        // 中,按下,选中效果。
        public StateListDrawable setbg(Integer[] mImageIds) {
            StateListDrawable bg = new StateListDrawable();
            Drawable normal = this.getResources().getDrawable(mImageIds[0]);
            Drawable selected = this.getResources().getDrawable(mImageIds[1]);
            Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
            bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
            bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
            bg.addState(View.ENABLED_STATE_SET, normal);
            bg.addState(View.FOCUSED_STATE_SET, selected);
            bg.addState(View.EMPTY_STATE_SET, normal);
            return bg;
        }
    }
}



XML代码:

在res/drawable下面新建mybutton_background.xml文件,内容如下:


<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=”http://schemas.android.com/apk/res/android“>   
<item android:state_focused=”true”      
         android:state_pressed=”false”    
         android:drawable=”@drawable/yellow” />  
<item android:state_focused=”true”      
          android:state_pressed=”true”     
          android:drawable=”@drawable/green” />
<item android:state_focused=”false”      
          android:state_pressed=”true”
          android:drawable=”@drawable/blue” /> 
<item android:drawable=”@drawable/grey” />
</selector>


这里面就定义了在不同状态下的显示图片,然后在layout里面定义Button的时候,指定它的background为这个mybutton_background


<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical”
        android:layout_width=”fill_parent”
        android:layout_height=”fill_parent”
        >
    <Button android:id=”@+id/btn”
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:text=”@string/mybtn”
            android:background=”@drawable/mybutton_background” />
</LinearLayout>