概述

在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。

selector中的常用属性

  • android:state_selected 控件选中状态,可以为true或false
  • android:state_focused 控件获得焦点状态,可以为true或false
  • android:state_pressed 控件点击状态,可以为true或false
  • android:state_enabled 控件使能状态,可以为true或false
  • android:state_checkable 控件可勾选状态,可以为true或false
  • android:state_checked 控件勾选状态,可以为true或false

注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。

  • android:window_focused 应用程序窗口焦点状态,可以为true或false
  • android:color 定义特定状态的颜色
    • 为16进制颜色。这个颜色由rgb值指定,可带alpha,必须以”#“开头,后面跟随alpha-red-green-blue信息,格式可以为:
      • #rgb
      • #argb
      • #rrggbb
      • #aarrggbb

使用selector设置背景

把下面的XML保存成.xml文件(比如list_item_bg.xml),运行时系统会根据ListView中列表项的状态来使用相应的背景图片。

drawable/list_item_bg.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>

使用方法

  • 第一种是在listview中配置android:listSelector=”@drawable/list_item_bg”
  • 第二种是在listview的item中添加属性android:background=”@drawable/list_item_bg”
  • 第三种是java代码中使用:
    Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);listview.setSelector(drawable);

注:列表有时候为黑的情况,需要加上下面的代码使其透明:

android:cacheColorHint="@android:color/transparent"

使用selector设置字体颜色

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="#FF0000" />    <item android:state_focused="true" android:color="#00FF00" />    <item android:state_pressed="true" android:color="#0000FF" />    <item android:color="#000000" /></selector>

使用方法

android:textColor="@drawable/button_color"

更复杂的效果

还可以实现更复杂的效果,例如渐变等等。 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> 

使用方法

android:background="@drawable/button_color"android:focusable="true"

android按钮被点击文字颜色变化效果

转自:http://blog.csdn.net/maylian7700/article/details/6978131

有的时候做应用需要点击按钮时文字颜色也跟着变,松开后又还原,目前发现两种解决方案:第一用图片,如果出现的地方比较多,那么图片的量就相当可观;第二,也就是本文讲到的。废话少说,先贴图片,再上代码。

正常效果:

按下效果:

先在values目录创建color.xml文件,在里面加入以下自定义颜色(注意不是用color标签)的代码:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <drawablename="red">#f00</drawable>
  4. <drawablename="green">#0f0</drawable>
  5. <drawablename="gray">#ccc</drawable>
  6. </resources>
    


然后在res下新建drawable目录,里面新建btn_bg.xml和btn_color.xml文件,代码如下:

btn_bg.xml

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:state_window_focused="false"android:state_enabled="true"
  4. android:drawable="@drawable/btn_test_normal"/>
  5. <itemandroid:state_enabled="false"android:drawable="@drawable/btn_test_normal"/>
  6. <itemandroid:state_pressed="true"android:drawable="@drawable/btn_test_press"/>
  7. <itemandroid:state_focused="true"android:drawable="@drawable/btn_test_normal"/>
  8. </selector>


btn_color.xml

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:state_focused="false"android:state_enabled="true"android:state_pressed="false"
  4. android:color="@drawable/red"/>
  5. <itemandroid:state_enabled="false"android:color="@drawable/gray"/>
  6. <itemandroid:state_pressed="true"android:color="@drawable/green"/>
  7. <itemandroid:state_focused="true"android:color="@drawable/red"/>
  8. </selector>

最后是测试用的布局文件:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@android:color/white"
  7. >
  8. <Button
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="按下文字会变效果"
  12. android:textColor="@drawable/btn_color"
  13. android:background="@drawable/btn_bg"
  14. />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="按钮被禁用"
  19. android:enabled="false"
  20. android:textColor="@drawable/btn_color"
  21. android:background="@drawable/btn_bg"
  22. />
  23. </LinearLayout>


更多相关文章

  1. Android客制化------在设置中加入RAM flash计算
  2. Android(安卓)XML设置圆角边框
  3. Android学习笔记:布局
  4. Android(安卓)录制mp3使用mp3lame 库
  5. Android实战简易教程(自定义控件实现数字液晶时钟Demo)
  6. Drawable专讲 Android
  7. Android(安卓)Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解
  8. [Android(安卓)Training视频系列]2.4 Recreating an Activity
  9. Android开发工程师个人简历

随机推荐

  1. 利用ROW_NUMBER() OVER函数给SQL数据库中
  2. SQLServer数据库的各种管理方法
  3. SQL SERVER数据库的作业的脚本及存储过程
  4. SQL SERVER备份数据库存储过程的方法
  5. SQL Server比较常见数据类型详解
  6. 深入浅析SQL Server 触发器
  7. 配置SQL Server数据库恢复模式(2种方法)
  8. 如何解决在Azure上部署Sqlserver网络访问
  9. MSSQL中进行SQL除法运算结果为小数却显示
  10. SQL Server查询前N条记录的常用方法小结