Android中的Selector的用法详解及实例
作者:lqh 时间:2023-11-26 22:17:06
Android中的Selector的用法
<?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/bg_selected">
</item>
<item android:drawable="@drawable/bg_unselect">
</item>
</selector>
在工作的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片。开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点击了按钮后,按钮没一点反应。于是我就再给控件添加上onTouch监听事件,按下后改变背景颜色,松手后再恢复原来颜色。但后来发现了selector这个利器,真是喜出望外,不用再添加onTouch监听事件了,用起来也方便灵活。不得不说,多和其他开发人员交流技术经验等还是很有必要的,特别是像我这样独自负责一个app开发的。
Android的selector要在 drawable 下配置。
其中,selector可以设置的属性有:
android:state_pressed 如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。
android:state_focused 如果是true,获得焦点时显示;如果是false没获得焦点显示默认。
android:state_selected 如果是true,当被选择时显示该图片;是false未被选择时显示该图片。
android:state_checkable 如果值为true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。
android:state_checked 如果值为true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。
android:state_enabled 如果值为true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
android:state_window_focused 如果值为true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片
这些属性值也可以叠加使用,比如:
android:state_window_focused=”true” android:state_pressed=”true”
表明是非触摸模式下获得焦点并单击时的背景图片。
一般来说,Button控件只需要用 android:state_pressed就可以。比如,一个Button控件的背景设置为:
<?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/bg_selected">
</item>
<item android:drawable="@drawable/bg_unselect">
</item>
</selector>
这表明该Button控件按下时,背景图片是bg_selected,其它情况下(没有被点击或者点击后松开等)背景图片为bg_unselect。
selector除了可以设置组件的背景颜色外,也可以设置文字的颜色。比如某个Button控件被按下后,Button上的文字颜色也发生改变。例如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#ffffff"></item>
<item android:color="#000000"></item>
</selector>
即该Button控件的颜色为黑色,但按钮按下后,颜色会变成白色。松开后,颜色恢复为黑色。
以上可以在代码中这样设置:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_slector"
android:text="abc"
android:textColor="@drawable/text_selector"
/>
除了selector外,还有一个Shape,可以用来定制控件的图形效果,两者可以一起使用。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/marvel__dead/article/details/66974755
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java语法中Lambda表达式无法抛出异常的解决
Android实现流动的渐变色边框效果
![](https://img.aspxhome.com/file/2023/9/117129_0s.png)
详解spring boot rest例子
![](https://img.aspxhome.com/file/2023/1/73041_0s.jpg)
C#中使用Join与GroupJoin将两个集合进行关联与分组
![](https://img.aspxhome.com/file/2023/2/119522_0s.jpg)
Java8时间api之LocalDate/LocalDateTime的用法详解
C# FileStream实现大文件复制
![](https://img.aspxhome.com/file/2023/8/96348_0s.png)
java 重定义数组的实现方法(与VB的ReDim相像)
java线程之用Thread类创建线程的方法
关于java数组与字符串相互转换的问题
C# 异步多线程入门基础
![](https://img.aspxhome.com/file/2023/8/85668_0s.png)
c#根据网址抓取网页截屏生成图片的示例
spring mvc中@RequestBody注解的作用说明
![](https://img.aspxhome.com/file/2023/4/62764_0s.png)
Android中使用Gson解析JSON数据的两种方法
java导出大批量(百万以上)数据的excel文件
浅析Spring Boot中的spring-boot-load模块
![](https://img.aspxhome.com/file/2023/6/58756_0s.png)
SpringBoot创建RSocket服务器的全过程记录
详解Kotlin中的面向对象(二)
C#与PLC通讯的实现代码
![](https://img.aspxhome.com/file/2023/0/111280_0s.png)
SpringBoot深入了解日志的使用
![](https://img.aspxhome.com/file/2023/7/65177_0s.png)