在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等几个状态:
android:state_pressed
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。
android:state_focused
Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.
true,获得焦点时显示;false,没获得焦点显示默认。
android:state_selected
Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.
true,当被选择时显示该图片;false,当未被选择时显示该图片。
android:state_checkable
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。
android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。
android:state_enabled
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
android:state_window_focused
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。
<?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/button_pressed"/><!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused"/><!-- focused -->
<item android:drawable="@drawable/button_normal"/><!-- default -->
</selector>
一.创建xml文件,位置:drawable/xxx.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文件:
1.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加属性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是设置是否响应事件,指所有事件
根据这些状态同样可以设置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:background="@drawable/button_color"
分享到:
相关推荐
本篇文章是对android背景选择器selector的用法进行了详细的总结与分析,需要的朋友参考下
浅谈android的selector背景选择器
本篇文章是对Android中selector背景选择器的使用进行了详细的分析介绍,需要的朋友参考下
http://blog.csdn.net/zhufuing/article/details/20134255博客的demo下载
Android自定义矩形及selector、shape的使用Android自定义矩形及selector、shape的使用
本文实例讲述了Android编程中selector背景选择器用法。分享给大家供大家参考,具体如下: 在Android开发过程中,经常对某一View的背景在不同的状态下,设置不同的背景,增强用户体验。如果按钮,在按下时,背景变化...
一个强大的selector注入器,它可以让view自动产生selector状态,免去了你写selector的麻烦。
Android selector 完整demo
* 选择器关闭后,调用回调事件接口 */ public interface OnSelectorCancel { /** * @param proName 省份名 * @param cityName 城市名 * @param regName 区县名 */ public void onSelectorResult...
Contact Selector 联系人选择器,选择联系人,Choose Contact,Contact,Contact select
ColorSelector4Android是安卓平台下的颜色选择器 使用Android Studio开发,依赖本库也只能在Android Studio,API等级为19 已封装好Activity、DialogFragment、AlertDialog等形式供直接使用 也可以直接在自己的程序...
Android-Ultra-Photo-Selector是一个多图片选择的开源框架,可以实现多图片的选择
Java_Picture Selector Library for Android or 图片选择器
主要介绍了Android编程之selector下设置背景属性值的方法,结合实例形式分析了Android的selector背景选择器相关使用技巧,需要的朋友可以参考下
主要为大家详细介绍了Android selector状态选择器的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ListView Button ImageView 里应用selector选择器切换图片并保持住
简单集成了Android-Ultra-Photo-Selector这个框架的使用,非常感谢作者,框架地址:https://github.com/AizazAZ/Android-Ultra-Photo-Selector
Android文字颜色背景触摸点击反馈色之selector.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。