Android开发(六)——组件颜色Selector(Selector与Shape的基本用法 )
andorid控件改变状态时改变颜色,使用selector。
<?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>
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</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>
实例:
先在values目录创建color.xml文件,在里面加入以下自定义颜色(注意不是用color标签)的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="red">#f00</drawable>
<drawable name="green">#0f0</drawable>
<drawable name="gray">#ccc</drawable>
</resources>
然后在res下新建drawable目录,里面新建btn_bg.xml和btn_color.xml文件,代码如下:
btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_test_normal" />
<item android:state_enabled="false" android:drawable="@drawable/btn_test_normal" />
<item android:state_pressed="true" android:drawable="@drawable/btn_test_press" />
<item android:state_focused="true" android:drawable="@drawable/btn_test_normal" />
</selector>
btn_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_enabled="true" android:state_pressed="false"
android:color="@drawable/red" />
<item android:state_enabled="false" android:color="@drawable/gray" />
<item android:state_pressed="true" android:color="@drawable/green" />
<item android:state_focused="true" android:color="@drawable/red" />
</selector>
最后是测试用的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按下文字会变效果"
android:textColor="@drawable/btn_color"
android:background="@drawable/btn_bg"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮被禁用"
android:enabled="false"
android:textColor="@drawable/btn_color"
android:background="@drawable/btn_bg"
/> </LinearLayout>
参考:
http://blog.csdn.net/maylian7700/article/details/6978131
http://www.2cto.com/kf/201110/107866.html
http://developer.android.com/guide/topics/resources/color-list-resource.html
http://blog.csdn.net/lzt623459815/article/details/8538784
Android开发(六)——组件颜色Selector(Selector与Shape的基本用法 )的更多相关文章
- 一个帖子学会Android开发四大组件
来自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 这个文章主要是讲Android开发的四大组件,本文主要分为 一.Act ...
- 一天就学会Android开发四大组件
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity详解二.Service详解三.Broadcast Receiver详解四.Content Provider详解外加一个重 ...
- 关于Android开发四大组件
文章主要是讲Android开发的四大组件,本文主要分为 文章源自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 一.Act ...
- Android开发四大组件概述
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity具体解释 二.Service具体解释 三.Broadcast Receiver具体解释 四.Content Provid ...
- Android开发之组件
Android应用程序由组件组成,组件是可以解决被调用的基本功能模块.Android系统利用组件实现程序内部或程序间的模块调用,以解决代码复用问题,这是Android系统非常重要的特性.在程序设计时, ...
- Android开发四大组件之Service(具体解释篇)
Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...
- Android 开发 系统组件集合
常用的TextView.Button.ImageView和几个常用布局就不介绍了,我们介绍一些特别好用但是常常忘记的组件. 标题栏组件 <androidx.appcompat.widget.To ...
- Android开发----Button组件的使用与练习
Button 学习目标: 文字大小.颜色 自定义背景形状 自定义按压效果 点击事件 创建一个新的Activity以增加控件 1.文字大小.颜色 直接在xml文件中定义即可 <Button and ...
- android开发常用组件【持续更新中。。。】
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- 从零开始学android开发-四大组件之一 Activity
1.Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满屏的, ...
随机推荐
- HDU 3032 Nim or not Nim? (sg函数)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 【MyBatis】MyBatis之如何配置
1,MyBatis简介 MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简 ...
- Linux下安装LAMP(Apache+PHP+MySql)和禅道
1.更新yum源: yum update -y 2.安装Apache+PHP+MySql yum install httpd mysql-devel mysql-server mysql-php ph ...
- Scala学习网址
scala学习网址为:https://twitter.github.io/scala_school/zh_cn https://www.zhihu.com/question/26707124
- 最优化方法:共轭梯度法(Conjugate Gradient)
http://blog.csdn.net/pipisorry/article/details/39891197 共轭梯度法(Conjugate Gradient) 共轭梯度法(英语:Conjugate ...
- JEECG中t:dictSelect的extendJson用法
1.t:dictSelect的各个属性值如下: 属性名 类型 描述 是否必须 默认值 title string 标题 否 null field string 控件字段名字 是 null typeGro ...
- JS charCodeAt在PHP中的等价物(完整的unicode和表情符号兼容性)
我在JS中有一个简单的代码,如果涉及特殊字符,我无法在PHP中复制. 这是JS代码(请参阅JSFiddle输出): var str = "t
- Android基础知识之Manifest中的Intent-filter元素
原文:http://android.eoe.cn/topic/android_sdk :指定活动.服务.或者广播接收者能支持的intent的类型.一个意图过滤器声明了其父组件的能力——一个活动或者服务 ...
- OpenCV Machine Learning 之 K近期邻分类器的应用 K-Nearest Neighbors
OpenCV Machine Learning 之 K近期邻分类器的应用 以下的程序实现了对高斯分布的点集合进行分类的K近期令分类器 #include "ml.h" #includ ...
- [na]计算机网络性能指标(延迟/吞吐量/RTT等)
参考 计算机网络性能指标 计算机网络性能指标 带宽.速率.延迟.吞吐量.丢包率(无线验收标准一般-75dbm,del<100ms,丢包率3%) 带宽x延迟 决定着路上的数据的多少. 速率: 连接 ...