您的位置:首页 > 移动开发 > Android开发

Android开发之状态选择器(selector)详解

2014-12-23 21:03 302 查看
这些天博主的项目进度比较快,现在服务器那端进度较慢,所以博主晚上最近会比较有时间,所以最近会多写些博客出来和大家分享~希望能够帮助到大家。

所谓状态选择器,就是控件(view或者viewgroup)的状态发现变化的时候,我们可以再指定的状态下,切换控件的背景属性(background),从而达到效果绚丽的目的。

先随便看一个例子:

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

<item android:drawable="@drawable/btn_langage_content" android:state_focused="true"></item>
<item android:drawable="@drawable/btn_langage_content" android:state_pressed="true"></item>
<item android:drawable="@drawable/btn_langage_content" android:state_selected="true"></item>
<item android:drawable="@drawable/btn_new_message"></item>

</selector>


可以看到状态选择器的写法一般为一个drawble属性加一个状态属性,当然,状态属性是可以写成多个的。那么,我们先来讲讲有哪些常用的状态属性。

1、android:state_focused 官方解释为当前view获得焦点的时候

2、android:state_window_focused 当前view所在的窗体获得焦点的时候

3、android:state_enabled 当前view可以被点击或者触发触摸事件的时候

4、android:state_checkable 当前view是否可以被check,比如checkBox,RadioButton是可以check的

5、android:state_checked 当前view是否被check 比如checkBox被check的

6、android:state_selected 当前view是否被选中

7、android:state_pressed 当前view是否被按下

8、android:state_activated 这个比较难解释,官方解释是set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.

9,什么都不写,就是正常状态下

在讲解完状态属性之后,我们就可以根据实际需要来配置状态选择器了。比如一个button,我们可以设定它被按下时的颜色。比如一个listView,我们可以设定选中某一个item时,item的背景颜色。只要在对应的状态值下,传入对应的drawble资源即可。

关于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点的坐标的相对位置。

这里博主就不上传效果图了,希望这些能够帮助到大家。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: