设置Button按钮状态背景

来源:互联网 发布:服务器数据恢复 沈阳 编辑:程序博客网 时间:2024/05/16 16:01

Android selector选择器可以让你切换自定义的背景风格,让你的控件或者布局在不同状态下背景切换,背景可以使眼色或者图片资源。


首先,android中的selector要在res/drawable/xxx.xml中配置,比如下面Button的例子:

使用drawable:
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">    
  3.         <!-- 点击时的背景图 -->  
  4.     <item android:state_pressed="true"    
  5.         android:drawable="@drawable/item_bg_s" />    
  6.     <!-- 获取焦点时的背景图 -->  
  7.     <item android:state_focused="true"    
  8.         android:drawable="@drawable/item_bg_s" />    
  9.     <!-- 默认的背景图 -->  
  10.     <item android:drawable="@drawable/item_bg_n" />    
  11. </selector>   


这个在Java代码中的实现方式是:

[java] view plain copy
  1. Integer[] mButtonState = { R.drawable.defaultbutton,    
  2.                 R.drawable.focusedpressed, R.drawable.pressed };    
  3.         Button mButton = (Button) findViewById(R.id.button);    
  4.         mButton.setBackgroundDrawable(myButton.setbg(mButtonState));    
  5.     
  6.         public StateListDrawable setbg(Integer[] mImageIds) {    
  7.                 StateListDrawable bg = new StateListDrawable();    
  8.                 /*默认背景图*/  
  9.                 Drawable normal = this.getResources().getDrawable(mImageIds[0]);    
  10.                 /*选择时的背景图*/  
  11.                 Drawable selected = this.getResources().getDrawable(mImageIds[1]);    
  12.                 /*按下时的背景图*/  
  13.                 Drawable pressed = this.getResources().getDrawable(mImageIds[2]);    
  14.                 /*背景图绑定按钮各个状态*/  
  15.                 bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);    
  16.                 bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);    
  17.                 bg.addState(View.ENABLED_STATE_SET, normal);    
  18.                 bg.addState(View.FOCUSED_STATE_SET, selected);    
  19.                 bg.addState(View.EMPTY_STATE_SET, normal);    
  20.                 return bg;    
  21.         }   


使用color:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.         <!-- 选中时的颜色 -->  
  4.     <item android:state_selected="true" android:color="#0FF" />  
  5.     <!-- 获得焦点时的颜色 -->  
  6.     <item android:state_focused="true" android:color="#F0F" />  
  7.     <!-- 点击时的颜色 -->  
  8.     <item android:state_pressed="true" android:color="#FF0" />  
  9.     <!-- 默认的颜色 -->  
  10.     <item android:color="#000" />  
  11. </selector>  

这个方法可以用来设置Button背景的选择效果,也可以用来设置Button上面文字的选择效果。
最后,将设置好的选择器设置到Button的background中去。

也可以为button增加style,如下

[html] view plain copy
  1. <Button  
  2.         android:id="@+id/login"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:focusable="true"  
  6.         android:text="按钮"   
  7.         style="@style/ButtonText"/>  

然后在res/values下面定义这个style:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <resources>  
  3.         <style name="ButtonText">  
  4.             <item name="android:layout_width">fill_parent</item>  
  5.             <item name="android:layout_height">wrap_content</item>  
  6.             <item name="android:textColor">#ffffff</item>  
  7.             <item name="android:gravity">center</item>  
  8.             <!-- 阴影风格,如粗体,斜体 -->  
  9.             <item name="android:textStyle">bold</item>  
  10.             <!-- 阴影颜色,这里可以处理阴影,外发光,描边等效果 -->  
  11.             <item name="android:shadowColor">#000000</item>  
  12.             <!-- 阴影x偏移 -->  
  13.             <item name="android:shadowDx">1</item>  
  14.             <!-- 阴影y偏移 -->  
  15.             <item name="android:shadowDy">1</item>  
  16.             <!-- 阴影半径 -->  
  17.             <item name="android:shadowRadius">2</item>  
  18.             <!-- 背景 -->  
  19.             <item name="android:background">@drawable/customer_button_selector</item>  
  20.         </style>  
  21.     </resources>  

如果要为按钮加上渐变,圆角,描边等等,可以将selector结合使用shape,这样就能做出更加酷炫的按钮

shape定义按钮的方法和以上类似,可以定义一个selector结和shape:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- 按下状态 -->  
  4.     <item android:state_pressed="true">  
  5.         <shape>  
  6.             <solid   
  7.                 android:color="#80ff3434" />  
  8.             <padding   
  9.                 android:bottom="10dp"   
  10.                 android:left="30dp"   
  11.                 android:right="30dp"   
  12.                 android:top="10dp" />  
  13.         </shape>  
  14.         </item>  
  15.     <!-- 普通状态 -->  
  16.     <item>  
  17.         <shape android:shape="rectangle">  
  18.   
  19.             <gradient   
  20.                 android:endColor="#ffff1dae"   
  21.                 android:startColor="#ffff79ff"   
  22.                 android:type="linear" />             
  23.             <padding   
  24.                 android:bottom="10dp"   
  25.                 android:left="30dp"   
  26.                 android:right="30dp"   
  27.                 android:top="10dp" />  
  28.         </shape>  
  29.      </item>  
  30.   
  31. </selector>  



如发现设置不成功,请看下回分解:http://blog.csdn.net/androidtalent/article/details/52234473



0 0