当前位置: 代码迷 >> Android >> android背景选择器selector用法汇总
  详细解决方案

android背景选择器selector用法汇总

热度:30   发布时间:2016-04-28 02:12:05.0
android背景选择器selector用法汇总 .

一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片

?

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <selector?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ??
  4. ????<!--?没有焦点时的背景图片?-->??
  5. ????<item?android:drawable="@drawable/handle_normal"?android:state_enabled="true"?android:state_window_focused="false"/>??
  6. ????<!--?触摸模式按下时的背景图片?-->??
  7. ????<item?android:drawable="@drawable/handle_pressed"?android:state_pressed="true"/>??
  8. ????<item?android:drawable="@drawable/handle_focused"?android:state_enabled="true"?android:state_focused="true"/>??
  9. ????<item?android:drawable="@drawable/handle_normal"?android:state_enabled="true"/>??
  10. ????<!--?获得焦点时的图片背景?-->??
  11. ????<item?android:drawable="@drawable/handle_focused"?android:state_focused="true"/>??
  12. ??
  13. ????<!--??
  14. ????android:state_selected是选中??
  15. ????android:state_focused是获得焦点??
  16. ????android:state_pressed是点击??
  17. ????android:state_enabled是设置是否响应事件,指所有事件??
  18. ????-->??
  19. ??
  20. </selector>??
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 没有焦点时的背景图片 -->    <item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>    <!-- 触摸模式按下时的背景图片 -->    <item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/>    <item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>    <item android:drawable="@drawable/handle_normal" android:state_enabled="true"/>    <!-- 获得焦点时的图片背景 -->    <item android:drawable="@drawable/handle_focused" android:state_focused="true"/>    <!--    android:state_selected是选中	android:state_focused是获得焦点	android:state_pressed是点击	android:state_enabled是设置是否响应事件,指所有事件    --></selector>

.使用xml文件:

?

1.方法一:listview中配置android:listSelector="@drawable/xxx
或者在listviewitem中添加属性android
background="@drawable/xxx"

2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);??
???????ListView.setSelector(drawable);
但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。

相关属性:

android:state_selected是选中
android:state_focused
是获得焦点
android:state_pressed
是点击
android:state_enabled
是设置是否响应事件,指所有事件

根据这些状态同样可以设置buttonselector效果。也可以设置selector改变button中的文字状态。


以下是配置button中的文字效果:
drawable/button_font.xml

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <selector?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<item?android:state_selected="true"?android:color="#FFF"?/>??
  4. ????<item?android:state_focused="true"?android:color="#FFF"?/>??
  5. ????<item?android:state_pressed="true"?android:color="#FFF"?/>??
  6. ????<item?android:color="#000"?/>??
  7. </selector>??
<?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

?

?

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <selector?xmlns:android="http://schemas.android.com/apk/res/android">?????????/???
  3. <item?android:state_pressed="true">//定义当button?处于pressed?状态时的形态。???
  4. ????????????????<shape>??
  5. ????????????????<gradient??android:startColor="#8600ff"?/>???
  6. ??????????????????????<stroke???android:width="2dp"?android:color="#000000"?/>???
  7. ???????????????????????<corners?android:radius="5dp"?/>????
  8. ???????????????????????<padding?android:left="10dp"?android:top="10dp"???
  9. ????????????????????????????????android:bottom="10dp"?android:right="10dp"/>????
  10. ?????????????????</shape>???
  11. </item>???
  12. <item?android:state_focused="true">//定义当button获得?focus时的形态???
  13. ?????????????????<shape>???
  14. ???????????????????????<gradient?android:startColor="#eac100"/>???
  15. ???????????????????????<stroke?android:width="2dp"?android:color="#333333"??color="#ffffff"/>???
  16. ???????????????????????<corners?android:radius="8dp"?/>?????
  17. ???????????????????????<padding?android:left="10dp"?android:top="10dp"???
  18. ????????????????????????????????android:bottom="10dp"?android:right="10dp"/>?????????????????????
  19. ????????????????</shape>???
  20. ?</item>??
  21. </selector>
  22. <?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>

?

最后,需要在包含?buttonxml文件里添加两项。例如main.xml?文件,需要在<Button />里加两项androidfocusable="true"?android:background="@drawable/button_color"这样就把drawale/button_color.xml文件与这个控件关联起来了。
  相关解决方案