shape和selector是Android UI中经常用到的,比如我们自定义一个圆角Button,点击Button有什么效果变化,就要使用到shape+selector。可以这样说,shape+selector在美化控件过程中有着十分重要的作用。
1、shape
作用:XML中定义的几何形状
位置:res/drawable/文件名称.xml
使用方法:java代码中:R.drawable.文件名称
XML中:Android:background="@drawable/文件名称"
属性:<shape>:Android:shape=[“rectangle” | “oval” | “line” | "ring"]; rectangle为矩形,oval为椭圆,line为直线,ring为环形
<shape>子节点属性:
<gradient>渐变
android:startColor 起始颜色
android:endColor 结束颜色
android:angle 渐变角度,0从上到下,90表示从左到右,数值必须为45的整数倍,默认为0
android:type 渐变的样式,liner为线性渐变 radial为环形渐变 必须指定渐变角度 android:gradientRadius
<solid>填充
android:color 填充的颜色
<stroke>描边
android:width 描边的宽度
android:color 描边的颜色
android:dashWidth 表示'-'的宽度
android:dashGap 表示'-'的距离
<corners>圆角
android:radius 圆角半径 值越大角越圆
android:topRightRadius 右上圆角半径
android:bottomLeftRadius 右下圆角半径
android:topLeftRadius 左上圆角半径
android:bottomRightRadius 左下圆角半径
2、selector
位置:res/drawable/文件名称.xml
使用方法:java代码中:R.drawable.文件名称
XML中:Android:background="@drawable/文件名称"
<span style="white-space:pre"> </span><?xml version="1.0" encoding="utf-8" ?>
<span style="white-space:pre"> </span><selector xmlns:Android="http://schemas.android.com/apk/res/android">
<span style="white-space:pre"> </span> <!-- 默认时的背景图片-<pre name="code" class="java"><?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:Android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item Android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item
Android:state_window_focused="false"
android:drawable="@drawable/pic_blue"
/>
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item
Android:state_focused="true"
android:state_pressed="true"
android:drawable= "@drawable/pic_red"
/>
<!-- 触摸模式下单击时的背景图片-->
<item
Android:state_focused="false"
Android:state_pressed="true"
Android:drawable="@drawable/pic_pink"
/>
<!--选中时的图片背景-->
<item
Android:state_selected="true"
android:drawable="@drawable/pic_orange"
/>
<!--获得焦点时的图片背景-->
<item
Android:state_focused="true"
Android:drawable="@drawable/pic_green"
/>
</selector>
->