数据结构

Button按钮状态背景的设置

Android selector选择器可以让你切换自定义的背景风格,让你的控件或者布局在不同状态下背景切换,背景可以使眼色或者图片资源。   首先,android中的selector要在res/drawable/xxx.xml中配置,比如下面Button的例子: 使用drawable:
  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代码中的实现方式是:
  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.         public StateListDrawable setbg(Integer[] mImageIds) {
  6.                 StateListDrawable bg = new StateListDrawable();
  7.                 /*默认背景图*/
  8.                 Drawable normal = this.getResources().getDrawable(mImageIds[0]);
  9.                 /*选择时的背景图*/
  10.                 Drawable selected = this.getResources().getDrawable(mImageIds[1]);
  11.                 /*按下时的背景图*/
  12.                 Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
  13.                 /*背景图绑定按钮各个状态*/
  14.                 bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
  15.                 bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
  16.                 bg.addState(View.ENABLED_STATE_SET, normal);
  17.                 bg.addState(View.FOCUSED_STATE_SET, selected);
  18.                 bg.addState(View.EMPTY_STATE_SET, normal);
  19.                 return bg;
  20.         }
  使用color:
  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,如下
  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:
  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:
  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.             <gradient
  19.                 android:endColor="#ffff1dae"
  20.                 android:startColor="#ffff79ff"
  21.                 android:type="linear" />
  22.             <padding
  23.                 android:bottom="10dp"
  24.                 android:left="30dp"
  25.                 android:right="30dp"
  26.                 android:top="10dp" />
  27.         </shape>
  28.      </item>
  29. </selector>
源码下载: http://download.csdn.net/detail/zoeice/4420225