Android资源文件Res详解-StateListDrawable

  上节,我们学习了ShapeDrawable的用法,可以绘制简单图形,较引用ImageView方便。今天,博客中讲解StateListDrawable状态列表的使用,也就是常说的selector(选择器)。
  selector可分为两类,一类是color-selector,颜色选择器,资源文件位于res/color文件夹下(该文件夹非默认,需要手动建立),常用在控件背景颜色和文本颜色变化,即随着控件状态改变(selected、pressed、checked、checkable、default等等)而即时改变颜色。
  另一类是drawable-selector,背景图选择器,资源文件位于res/drawable文件夹下,常用在控件背景图变化、颜色变化和图片源变化,即随着控件状态改变而改变背景图片或颜色。
  

1.color-selector

       语法

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="hex_color"               //颜色值,#RGB,$ARGB,#RRGGBB,#AARRGGBB
    android:state_pressed=["true" | "false"]//是否触摸
    android:state_pressed=["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 : android:textColor="@color/text_color"
  代码:text.setTextColor(R.color.text_color);
  或者 使用ColorStateList
      int[] colors = new int[] { color1, color2, color3, color4, color5, color6};  
      int[][] states = new int[6][];  
      states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };  
      states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };  
      states[2] = new int[] { android.R.attr.state_enabled };  
      states[3] = new int[] { android.R.attr.state_focused };  
      states[4] = new int[] { android.R.attr.state_window_focused };  
      states[5] = new int[] {};  
      ColorStateList colorList = new ColorStateList(states, colors); 
      text.setTextColor(colorList); 

  示例
  在res/color文件夹下建立文件 color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#ffffffff"/> <!-- pressed -->
    <item android:state_focused="true" android:color="#ffffffff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

<Button
    android:textColor="@color/color_selector"/>

2.drawable-selector

  语法
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]//drawable的大小是否当中状态变化,true表示是变化,false表示不变换,默认为false
    android:dither=["true" | "false"]//当位图与屏幕的像素配置不一样时(例如,一个ARGB为8888的位图与RGB为555的屏幕)会自行递色(dither)。设置为false时不可递色。默认true
    android:variablePadding=["true" | "false"] >//内边距是否变化,默认false
    <item
        android:drawable="@[package:]drawable/drawable_resource"//图片资源
        android:state_pressed=["true" | "false"]//是否触摸
        android:state_focused=["true" | "false"]//是否获取到焦点
        android:state_hovered=["true" | "false"]//光标是否经过
        android:state_selected=["true" | "false"]//是否选中
        android:state_checkable=["true" | "false"]//是否可勾选
        android:state_checked=["true" | "false"]//是否勾选
        android:state_enabled=["true" | "false"]//是否可用
        android:state_activated=["true" | "false"]//是否激活
        android:state_window_focused=["true" | "false"] />//所在窗口是否获取焦点
</selector>
  使用方法    
  xml :android:background="@drawable/bg_color"
  代码:button.setBackgound(R.drawable.bg_color);
      StateListDrawable bg = new StateListDrawable();  
      Drawable normal = context.getResources().getDrawable(R.drawable.normal);  
      Drawable pressed = context.getResources().getDrawable(R.drawable.pressed);  
      Drawable focused = context.getResources().getDrawable(R.drawable.focused);  
      Drawable unable = context.getResources().getDrawable(R.drawable.unable);  
      bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);  
      bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);  
      bg.addState(new int[] { android.R.attr.state_enabled }, normal);  
      bg.addState(new int[] { android.R.attr.state_focused }, focused); 
      bg.addState(new int[] { android.R.attr.state_window_focused }, unable);  
      bg.addState(new int[] {}, normal);  
      button.setBackground(bg);
         
  示例
  
  在res/drawable文件夹下建立 drawable_selector.xml文件
  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/button_bg_press" />
    <item android:state_focused="true" android:drawable="@drawable/button_bg_press" />
    <item android:state_pressed="true" android:drawable="@drawable/button_bg_press"  />
    <item android:drawable="@drawable/button_bg_normol"  />
</selector>
     
<Button
      android:background="@drawable/drawable_selector"/>
  selector是简洁的方式,实现View状态变化后背景与颜色变化的,可以省去很多逻辑代码,掌握了之后既可以省去很多Java代码,还能写一些漂亮的UI。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值