StateListDrawable可以根据View的不同状态,更换不同的背景

可以应用如EditText,Button等中,以Button为例

系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种实现可以用Java代码和XML实现

以Java代码:

 

Java代码 复制代码 收藏代码
  1. //……前面对Button的声明略去 
  2. okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); 
  3. cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); 
  4.  
  5. //……对应主要的代码 
  6. //当对应的View处于不同的状态时,对应的bacdground跟着变化 
  7.     private StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) { 
  8.         StateListDrawable sd = new StateListDrawable(); 
  9.         Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal); 
  10.         Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed); 
  11.         Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused); 
  12.         //注意该处的顺序,只要有一个状态与之相配,背景就会被换掉 
  13.         //所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了  
  14.         sd.addState(newint[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus); 
  15.         sd.addState(newint[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed); 
  16.         sd.addState(newint[]{android.R.attr.state_focused}, focus); 
  17.         sd.addState(newint[]{android.R.attr.state_pressed}, pressed); 
  18.         sd.addState(newint[]{android.R.attr.state_enabled}, normal); 
  19.         sd.addState(newint[]{}, normal); 
  20.         return sd; 
  21.     } 
//……前面对Button的声明略去
okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));

//……对应主要的代码
 //当对应的View处于不同的状态时,对应的bacdground跟着变化
    private StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) {
    	StateListDrawable sd = new StateListDrawable();
    	Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
    	Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
    	Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
    	//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
    	//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了 
    	sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
    	sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
    	sd.addState(new int[]{android.R.attr.state_focused}, focus);
    	sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
    	sd.addState(new int[]{android.R.attr.state_enabled}, normal);
    	sd.addState(new int[]{}, normal);
    	return sd;
    }

xml方式实现时,可以先了解下对应xml的语法

 

 

Xml代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <selectorxmlns:android="http://schemas.android.com/apk/res/android" 
  4.  
  5. android:constantSize=["true" | "false"] 
  6.  
  7. android:dither=["true" | "false"] 
  8.  
  9. android:variablePadding=["true" | "false"] > 
  10.  
  11. <item 
  12.  
  13. android:drawable="@[package:]drawable/drawable_resource" 
  14.  
  15. android:state_pressed=["true" | "false"] 
  16.  
  17. android:state_focused=["true" | "false"] 
  18.  
  19. android:state_selected=["true" | "false"] 
  20.  
  21. android:state_active=["true" | "false"] 
  22.  
  23. android:state_checkable=["true" | "false"] 
  24.  
  25. android:state_checked=["true" | "false"] 
  26.  
  27. android:state_enabled=["true" | "false"] 
  28.  
  29. android:state_window_focused=["true" | "false"] /> 
  30.  
  31. </selector> 
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"

android:constantSize=["true" | "false"]

android:dither=["true" | "false"]

android:variablePadding=["true" | "false"] >

<item

android:drawable="@[package:]drawable/drawable_resource"

android:state_pressed=["true" | "false"]

android:state_focused=["true" | "false"]

android:state_selected=["true" | "false"]

android:state_active=["true" | "false"]

android:state_checkable=["true" | "false"]

android:state_checked=["true" | "false"]

android:state_enabled=["true" | "false"]

android:state_window_focused=["true" | "false"] />

</selector>

 

下面对应的具体实例,由于是做背景用,该xml将放于/res/drawable下面(StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后

 

Xml代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <itemandroid:state_pressed="true"android:drawable="@drawable/btn_selected"/> 
  4.     <itemandroid:state_focused="true"android:drawable="@drawable/btn_selected"/> 
  5.     <itemandroid:state_enabled="true"android:drawable="@drawable/btn_normal"/> 
  6.     <item  android:drawable="@drawable/btn_normal"/> 
  7. </selector> 
  8.      
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_selected"/>
    <item android:state_focused="true" android:drawable="@drawable/btn_selected"/>
    <item android:state_enabled="true" android:drawable="@drawable/btn_normal"/>
    <item  android:drawable="@drawable/btn_normal" />
</selector>
    

在Button的xml中进行加载:

 

Xml代码 复制代码 收藏代码
  1. <Button 
  2.            android:id="@+id/canel" 
  3.            android:layout_width="wrap_content" 
  4.            android:layout_height="wrap_content"  
  5.            android:text="@string/btn_cancel" 
  6.            android:layout_margin="10dip" 
  7.            android:layout_weight="1" 
  8.            android:textColor="#ffffffff" 
  9.            android:textSize="15sp" 
  10.            android:background="@drawable/button_drawable" 
  11.            /> 
 <Button
            android:id="@+id/canel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/btn_cancel"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:textColor="#ffffffff"
            android:textSize="15sp"
            android:background="@drawable/button_drawable"
            />

或在java代码中加载:

 

 

Java代码 复制代码 收藏代码
  1. okBtn.setBackgroundDrawable(R.drawable.button_drawable); 
okBtn.setBackgroundDrawable(R.drawable.button_drawable);

效果都一样。

转自:http://yq135314.iteye.com/blog/1333511

posted on 2012-04-11 17:15  le7mo27  阅读(118)  评论(0)    收藏  举报