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

学习笔记_android之Selector与Shape的基本用法

2015-11-17 16:04 501 查看

1. Selector 介绍

drawable 的 item 中可以有以下属性:

android:drawable ="@drawable/drawable_resource" 放一个drawable资源

android:state_pressed =["true"| "false"] 是否按下,如一个按钮触摸或者点击。

android:state_focused =["true"| "false"] 是否取得焦点,比如用户选择了一个文本框。

android:state_selected =["true"| "false"] 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。

android:state_active =["true"| "false"]

android:state_checkable =["true"| "false"] 是否可选择,组件是否能被check。如:RadioButton是可以被check的。

android:state_checked =["true"| "false"] 选择,被checked了,如:一个RadioButton可以被check了。

android:state_enabled =["true"| "false"] 能够接受触摸或者点击事件

android:state_window_focused =["true"| "false"] 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性

android:state_activated 被激活

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。

2.Shape 介绍

下面介绍Shape的用法:

<shape> android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient> 渐变

android:startColor 起始颜色

android:endColor 结束颜色

android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;

android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid > 填充

android:color 填充的颜色

<stroke > 描边

android:width 描边的宽度

android:color 描边的颜色

android:dashWidth 表示'-'横线的宽度

android:dashGap 表示'-'横线之间的距离

<corners > 圆角

android:radius 圆角的半径 值越大角越圆

android:topRightRadius 右上圆角半径

android:bottomLeftRadius 右下圆角角半径

android:topLeftRadius 左上圆角半径

android:bottomRightRadius 左下圆角半径

3.用selector添加shape

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true">

<shape>

<gradient android:angle="270" android:endColor="#99BD4C"

android:startColor="#A5D245" />

<size android:height="60dp" android:width="320dp" />

<corners android:radius="8dp" />

</shape>

</item>

<item android:state_pressed="true">

<shape>

<gradient android:angle="270" android:endColor="#99BD4C"

android:startColor="#A5D245"/>

<size android:height="60dp" android:width="320dp" />

<corners android:radius="8dp" />

</shape>

</item>

<item>

<shape>

<gradient android:angle="270" android:endColor="#A8C3B0"

android:startColor="#C6CFCE"/>

<size android:height="60dp" android:width="320dp" />

<corners android:radius="8dp" />

</shape>

</item>

</selector>

4. 用法

第一种是在 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);

例:list_item_bg.xml

<?xml version="1.0"encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">

<shape>

<!-- 渐变 -->

<gradient

android:startColor="#ff8c00"

android:endColor="#FFFFFF"

android:type="radial"

android:gradientRadius="50"/>

<!-- 描边 -->

<stroke

android:width="2dp"

android:color="#dcdcdc"

android:dashWidth="5dp"

android:dashGap="3dp"/>

<!-- 圆角 -->

<corners

android:radius="2dp"/>

<padding

android:left="10dp"

android:top="10dp"

android:right="10dp"

android:bottom="10dp"/>

</shape>

</item>

<item android:state_focused="true">

<shape>

<gradient

android:startColor="#ffc2b7"

android:endColor="#ffc2b7"

android:angle="270"/>

<stroke

android:width="2dp"

android:color="#dcdcdc"/>

<corners

android:radius="2dp"/>

<padding

android:left="10dp"

android:top="10dp"

android:right="10dp"

android:bottom="10dp"/>

</shape>

</item>

<item>

<shape>

<solid android:color="#ff9d77"/>

<stroke

android:width="2dp"

android:color="#fad3cf"/>

<corners

android:topRightRadius="5dp"

android:bottomLeftRadius="5dp"

android:topLeftRadius="0dp"

android:bottomRightRadius="0dp"/>

<padding

android:left="10dp"

android:top="10dp"

android:right="10dp"

android:bottom="10dp"/>

</shape>

</item>

</selector>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: