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.        
  5.        
  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.        
  18.        
  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点的坐标的相对位置。

更多相关文章

  1. shape 的创建和使用
  2. Android(安卓)Launcher 分析
  3. Android(安卓)控件ProgressBar进度条
  4. Android中自定义控件
  5. Android中状态栏的隐藏
  6. android: layout_alignParentRight android_paddingRight
  7. 系出名门Android(5) - 控件(View)之TextView, Button, ImageButt
  8. 相对布局的属性
  9. Androidc学习笔记二之四大布局及碎片理解用法

随机推荐

  1. 在 Android(安卓)应用中使用数据库
  2. Android(安卓)与 JS 交互数据上限问题【R
  3. 面试例题4:绘制5行文本,每一行的字体大小逐
  4. Android(安卓)2.3 r1 中文API (78)―― Vie
  5. Android(安卓)输入系统
  6. 64位Ubuntu配置android环境报错(...adb":
  7. 活用Android的Message Queue(1/3)
  8. android中菜单以及自定义组件的使用
  9. eclipse中无法新建Android工程 出现问题:P
  10. android 7.1.1 软件升级安装报解析软件包