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

Android:Selector与Shape、layer-list

2015-05-06 20:42 363 查看
1. Selector

引用位置:res/drawable/文件的名称.xml

Java代码中:R.drawable.文件的名称

XML中:Android:background="@drawable/文件的名称"

item 中可以有以下属性:

[html] view
plaincopyprint?

android:drawable ="@drawable/drawable_resource"

android:state_pressed =["true"| "false"] 点击

android:state_focused =["true"| "false"] 获得焦点

android:state_selected =["true"| "false"] 选中

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

android:state_checkable =["true"| "false"] 是否可选择

android:state_checked =["true"| "false"] 选择

android:state_enabled =["true"| "false"] 是否响应事件,指所有事件

android:state_window_focused =["true"| "false"]
默认时的背景图片

例:

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

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

<item android:state_pressed="true" android:state_enabled="true"

android:drawable="@drawable/shape_topcorner_pressed"/>

<item android:drawable="@drawable/shape_topcorner_normal"/>

</selector>

注:<item>的选择类似于switch case语句

2. Shape

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

Java代码中:R.drawable.文件的名称

XML中:Android:background="@drawable/文件的名称"

属性:

shape=["rectangle" | "oval" | "line" | "ring"] 其中rectangle为矩形,oval为椭圆,line为水平直线,ring为环形

solid:实心,就是填充

gradient:渐变

[html] view
plaincopyprint?

android:startColor 起始颜色

android:endColor 结束颜色

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

渐变模式:

android:type="linear"
默认为线性渐变模式

android:type="radial"
径向渐变模式,需要指定半径

android:gradientRadius="50" 半径为50;

android:type="sweep"
环形渐变模式

stroke:描边

[html] view
plaincopyprint?

android:width="2dp" 描边的宽度

android:color 描边的颜色

还可以把描边弄成虚线的形式,设置方式为:

android:dashWidth="5dp"表示'-'这样一个横线的宽度

android:dashGap="3dp" 表示'-'之间隔开的距离

corners:圆角

[html] view
plaincopyprint?

android:radius 角的弧度,值越大角越圆

还可以把四个角设定成不同的角度:

<corners

android:topRightRadius="20dp"
右上角

android:bottomLeftRadius="20dp" 右下角

android:topLeftRadius="1dp" 左上角

android:bottomRightRadius="0dp" 左下角

/>

这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。

padding:填充

android:bottom="1.0dip" 底部填充

android:left="1.0dip" 左边填充

android:right="1.0dip" 右边填充

android:top="0.0dip" 上面填充


3.layer-list

简介:

将多个图片或上面两种效果按照顺序层叠起来。
例子:

[html] view
plaincopy





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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>

<bitmap android:src="@drawable/android_red"

android:gravity="center" />

</item>

<item android:top="10dp" android:left="10dp">

<bitmap android:src="@drawable/android_green"

android:gravity="center" />

</item>

<item android:top="20dp" android:left="20dp">

<bitmap android:src="@drawable/android_blue"

android:gravity="center" />

</item>

</layer-list>

[html] view
plaincopy





<ImageView

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:src="@drawable/layers" />

效果图:

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