StateListDrawable用于组织多个Drawable对象,顾名思义,StateList,它会随着目标组件状态(比如得到/失去焦点,勾选/未勾选,可用/不可用,按下/未按下,等等)的改变而自动切换

StateListDrawable对象的XML文件的根元素是<selector.../>,可包含多个<item.../>元素

下面是一个高亮显示正在输入的文本框的例子

创建一个普通Android xml文件,根元素选择selector,文件名为my_image,创建完以后把文件移动到res/drawable-mdpi文件夹下

[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="true"
  4. android:color="#f44"
  5. />
  6. <itemandroid:state_focused="false"
  7. android:color="#111"
  8. />
  9. </selector>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_focused = "true"          android:color = "#f44"     />    <item android:state_focused = "false"          android:color = "#111"     /></selector>

下面的主界面xml中引用上面定义的样式

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <EditText
  7. android:id="@+id/editText1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:textColor="@drawable/my_image"
  11. android:ems="10"/>
  12. <EditText
  13. android:id="@+id/editText2"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:textColor="@drawable/my_image"
  17. android:ems="10"/>
  18. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/editText1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="@drawable/my_image"        android:ems="10"  />    <EditText        android:id="@+id/editText2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="@drawable/my_image"        android:ems="10" /></LinearLayout>


效果如下,当焦点在文本框时,文字变为高亮显示


更多相关文章

  1. 图片布局在android中资源文件夹中添加一个新的图片资源
  2. Android(安卓)studio 配置Git (第一次提交代码)
  3. android中修改默认语言
  4. android桌面文件夹美化
  5. 如何制作Android(安卓)ROM
  6. Android(安卓)AIDL(Android(安卓)Interface Definition Language
  7. android目录结构介绍(寒假学习1)
  8. Android权限获取机制与常见权限不足问题分析
  9. Android(安卓)中使用Pull解析XML文件

随机推荐

  1. 一样的Android,不一样的学习
  2. Android平台开发指导(Android Porting Gui
  3. 新书内容连载(1):自定义Android组件之带图像
  4. Android工具箱之文件目录
  5. hello world 第一个android程序
  6. Android数据加密之Rsa加密
  7. C#开发Android应用的必备——Mono for An
  8. Android(安卓)应用设置全局Exception处理
  9. Android布局控件属性
  10. android使用android:ellipsize="end"无效