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的基本用法 )的更多相关文章

  1. 一个帖子学会Android开发四大组件

    来自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 这个文章主要是讲Android开发的四大组件,本文主要分为 一.Act ...

  2. 一天就学会Android开发四大组件

    这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity详解二.Service详解三.Broadcast Receiver详解四.Content Provider详解外加一个重 ...

  3. 关于Android开发四大组件

    文章主要是讲Android开发的四大组件,本文主要分为 文章源自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 一.Act ...

  4. Android开发四大组件概述

    这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity具体解释 二.Service具体解释 三.Broadcast Receiver具体解释 四.Content Provid ...

  5. Android开发之组件

    Android应用程序由组件组成,组件是可以解决被调用的基本功能模块.Android系统利用组件实现程序内部或程序间的模块调用,以解决代码复用问题,这是Android系统非常重要的特性.在程序设计时, ...

  6. Android开发四大组件之Service(具体解释篇)

    Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...

  7. Android 开发 系统组件集合

    常用的TextView.Button.ImageView和几个常用布局就不介绍了,我们介绍一些特别好用但是常常忘记的组件. 标题栏组件 <androidx.appcompat.widget.To ...

  8. Android开发----Button组件的使用与练习

    Button 学习目标: 文字大小.颜色 自定义背景形状 自定义按压效果 点击事件 创建一个新的Activity以增加控件 1.文字大小.颜色 直接在xml文件中定义即可 <Button and ...

  9. android开发常用组件【持续更新中。。。】

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  10. 从零开始学android开发-四大组件之一 Activity

    1.Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满屏的, ...

随机推荐

  1. 对simhash算法的一些思考

    http://2588084.blog.51cto.com/2578084/558873

  2. linux下yum命令出现Loaded plugins: fastestmirror Determining fastest mirrors

    今天yum install的时候出问题了,找了半天才找到一个可行的解决办法 fastestmirror是yum的一个加速插件,这里是插件提示信息是插件不能用了. 不能用就先别用呗,禁用掉,先yum了再 ...

  3. SQL Server 表分区之水平表分区

    什么是表分区? 表分区分为水平表分区和垂直表分区,水平表分区就是将一个具有大量数据的表,进行拆分为具有相同表结构的若干个表:而垂直表分区就是把一个拥有多个字段的表,根据需要进行拆分列,然后根据某一个字 ...

  4. 【MySQL】mysql中any,in,some,all的区别

    子查询就是指在一个select语句中嵌套另一个select语句. any,in,some,all分别是子查询关键词之一, any 可以与=.>.>=.<.<=.<> ...

  5. Linux下通过关键字模糊查找搜索文件

    [背景] 想要在Linux下面,找之前不知道放到哪里的一个tomcat的文件. [折腾过程] 1.最后是参考: linux查找文件命令find – 发芽的石头 – 博客频道 – CSDN.NET 去搜 ...

  6. linux手工释放内存

    先使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node.已延迟的块 I/O 和读写映射文件. 再执行下面任意一条命 ...

  7. WSAAsyncSelect模型触发不了FD_Close消息?

    原帖链接: http://bbs.csdn.net/topics/330164174 楼主: Server用WSAAsyncSelect模型,Client关闭程序时(不进行shutdown与close ...

  8. vmware虚拟机三种网络连接方式

    一.概述 vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式). 打开vmware虚拟机,我们可以在选项栏的“ ...

  9. Python把同一个对象循环赋值给另外一个变量

    Python把同一个对象循环赋值给另外一个变量,修改一个对象,其他对象也修改了 >>> row=['_'] * 3 >>> board = [] >>& ...

  10. nginx 反向代理说明

    1 在大型项目开发中,可能会有多个应用部署在不同机器上,如果想让用户访问单个域名或IP访问到这些应用,可以使用 nginx 的反向代理,将应用的地址通过 nginx 代理,用户通过访问 nginx 地 ...