Android:背景选择器selector及shape详解

这周是学校的创新创业周,一周没有课,正好可以有时间把Android基础知识都拿出来整理下。
刚学Android的时候,所有的界面都很丑,后来学了背景选择器,就大大改善下这种情况……恩,其实写起来挺费事的,因为在as或者eclipse中,有些内容没有提示,不能自动补全,这就让经常根据提示敲代码的我有点慌了……
so……这里正好总结一下,留作以后粘贴复制……嘿嘿。

所谓状态选择器,就是控件(view或者viewgroup)根据不同的选定状态来定义不同的现实效果,我们可以在指定的状态下,切换控件的背景属性(background),从而达到效果绚丽的目的。

常用属性:

属性状态
android:drawable放一个drawable资源
android:state_pressed当前view是否被按下,如一个按钮触摸或者点击。
android:state_focused官方解释为当前view获得焦点的时候,比如用户选择了一个文本框。
android:state_hovered光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable当前view是否可以被check。如:RadioButton是可以被check的。
android:state_checked被checked了,如:一个RadioButton可以被check了。
android:state_enabled当前view可以被点击或者触发触摸事件的时候,能够接受触摸或者点击事件
android:state_activated被激活,官方解释是set when a view or its parent has been “activated” meaning the user has currently marked it as being of interest.
android:state_window_focused当前view所在的窗体获得焦点的时候,应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

有的需要加android:focusable和android:clickable为true才能获取焦点

button_select.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/play_button"></item>
    <item android:state_focused="true" android:drawable="@drawable/play_button"></item>
    <item android:drawable="@drawable/button"></item>
</selector>  

button.xml默认效果

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#E53B36"/>
</shape>

play_button.xml点击效果

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#E8B714"/>
</shape>

常用checkbox设置:

<?xml version="1.0" encoding="UTF-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false"
     android:state_enabled="true" 
     android:state_checked="true" 
     android:drawable="@drawable/btn_check_on" />
    <item android:state_window_focused="false"
     android:state_enabled="true" 
     android:state_checked="false" 
     android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" 
    android:state_checked="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/btn_check_off_pressed" />
    <item android:state_focused="true" 
    android:state_enabled="true" 
    android:state_checked="true"
    android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_focused="true" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off_selected" />
    <item android:state_enabled="true"
     android:state_checked="false" 
     android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" 
     android:state_checked="true"
     android:drawable="@drawable/btn_check_on" />
</selector>

常用ImageButton设置:

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button2_down" />
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/button2_over" />
    <item android:state_enabled="true" android:drawable="@drawable/button2" />
</selector>

常用Button设置:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/login_input" />
    <item android:state_pressed="true" android:drawable="@drawable/login_input" />
    <item android:state_focused="true" android:drawable="@drawable/input_over" />
</selector>

设置TextView

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="@color/gray" android:state_pressed="false" android:state_enabled="true"/>
    <item android:color="@color/white" android:state_pressed="true" android:state_enabled="true"/>
    <item android:color="@color/gray" android:state_enabled="false"/>
</selector>

drawble资源,才是状态选择器的重点。首先,drawble资源比较复杂的时候,我们一般可以用.9patch图片来替代,完美适配。但比较简单的时候,我们可以自己去写背景的drawble资源的。那么,如何自定义一个drawble资源呢?下面来讲讲android下shape的使用。
先来看例子:

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="" >  

    <!-- 圆角 -->  
    <corners  
        android:radius="10dp"  
        android:topLeftRadius="" />  
    <!-- 描边 -->  
    <stroke  
        android:dashGap=""  
        android:dashWidth=""  
        android:width="2dp"  
        android:color="@color/darkgray" />  
    <!-- 实心 -->  
    <solid android:color="#c4c4c4" />  

    <!-- 大小 -->  
    <size  
        android:height=""  
        android:width="" />  
    <!-- 颜色渐变 -->  

    <gradient  
        android:angle=""  
        android:centerColor=""  
        android:endColor=""  
        android:gradientRadius=""  
        android:startColor=""  
        android:type="" />  
</shape>  

首先是最开始的shape标签。在shape里面有个shape属性,这个属性可以设定,也可以不设定,不设定的时候默认是矩形。设定有四个值可以设定:
1、rectangle 矩形
2、oval 椭圆形 当宽高设定为相同的时候,就是圆
3、line 线性形状
4、ring 环形 可用作数据刷新时转圈的提示
当设定为ring环形的时候,还需要设定一下几个属性

 android:innerRadiusRatio="3"  //浮点型数据,以环的宽度比率来表示内环的半径
 android:thicknessRatio="8"    //浮点型数据,以环的宽度比率来表示环的厚度
 android:useLevel="false"  //如果当做是LevlListDrawable使用时为true,其他为false

接下来定义在shape标签里面的节点

<!-- 圆角 -->  
   <corners  
       android:radius="10dp"  
       android:topLeftRadius="" />  

这个表示圆角,可以一次性设定四个边角的大小。也分个设定四个角度的大小,这里只写了全部的和左上角的。

<!-- 描边 -->  
<stroke  
   android:dashGap=""  
   android:dashWidth=""  
   android:width="2dp"  
   android:color="@color/darkgray" />  

这个表示描边,在边界画线。width表示线的厚度,color表示颜色,dashWidth和dashGap是用来画虚线的时候用的,dashWidth表示虚线的宽度,dashGap表示虚线的间隔。

<!-- 实心 -->  
  <solid android:color="#c4c4c4" />  

这个没什么好说的。

<!-- 大小 -->  
 <size  
     android:height=""  
     android:width="" />  

<!-- 颜色渐变 -->    
 <gradient  
     android:angle=""  
     android:centerColor=""  
     android:endColor=""  
     android:gradientRadius=""  
     android:startColor=""  
     android:centerX=""  
     android:centerY=""  
     android:gradientRadius=""  
     android:type="" />  

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值