Android中的状态选择器

来源:互联网 发布:linux telnet连接失败 编辑:程序博客网 时间:2024/04/29 21:32
Android中的状态选择器

概述
       在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
android:window_focused 应用程序窗口焦点状态,可以为true或false
android:color 定义特定状态的颜色
注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。

使用方法
第一种是在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"

demo分析:
   还可以实现更复杂的效果,例如渐变等等。 drawable/button_color.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <item android:state_pressed="true">
  4.         <!-- 定义当button 处于pressed 状态时的形态。-->
  5.         <shape>
  6.             <gradient android:startColor="#8600ff" />
  7.             <stroke android:width="2dp" 
  8.                     android:color="#000000" />
  9.             <corners android:radius="5dp" />  
  10.             <padding android:left="10dp" 
  11.                      android:top="10dp"
  12.                      android:bottom="10dp" 
  13.                      android:right="10dp"/>  
  14.         </shape>
  15.     </item>
  16.     <item android:state_focused="true">
  17.         <!-- 定义当button获得 focus时的形态 -->
  18.         <shape>
  19.             <gradient android:startColor="#eac100"/>
  20.             <stroke android:width="2dp" 
  21.                     android:color="#333333"  
  22.                     color="#ffffff"/>
  23.             <corners android:radius="8dp" />   
  24.             <padding android:left="10dp" 
  25.                      android:top="10dp"
  26.                      android:bottom="10dp" 
  27.                      android:right="10dp"/>
  28.         </shape>
  29.     </item>
  30. </selector> 

shape标签:
   在shape里面有个shape属性,这个属性可以设定,也可以不设定,不设定的时候默认是矩形。设定有四个值可以设定: 
1、rectangle 矩形
2、oval 椭圆形  当宽高设定为相同的时候,就是圆
3、line 线性形状
4、ring 环形  可用作数据刷新时转圈的提示
当设定为ring环形的时候,还需要设定一下几个属性
 android:innerRadiusRatio="3"  浮点型数据,以环的宽度比率来表示内环的半径
 android:thicknessRatio="8"    浮点型数据,以环的宽度比率来表示环的厚度
 android:useLevel="false"  如果当做是LevlListDrawable使用时为true,其他为false

gradient标签:
      angle表示颜色渐变的起始位置,0表示从左向右然后逆时针方向,90表示从上到下,以此类推,angle必须为45点整数倍
      startColor  endColor  centerColor,颜色 渐变 过程的颜色值。
      type,颜色渐变类型,有三个值
            1、linear,线性渐变,这个是默认值
            2、radial,放射性渐变,这个要配合android:gradientRadius属性使用,android:gradientRadius表示放射渐变的半径大小。
            3、sweep,扫描石渐变,就像雷达扫描的那个效果。
      centerX,centerY,表示渐变中心的X和Y点的坐标的相对位置。
0 0
原创粉丝点击