文章出处:https://blog.csdn.net/shift_wwx/article/details/38706989
目前发现shape是用于TextView、EditText、Button的background用,当然也可以配合selector使用,简单说就跟一般的图片是一样的,只不过这里变成了手动绘制了。
先show上code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFAEB9" />
<stroke
android:width="8dp"
android:color="@android:color/white"/>
<padding
android:left="20dp"
android:right="20dp"
android:top="20dp"
android:bottom="20dp"/>
<corners
android:radius="5dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
<gradient
android:startColor="#FFFFD700"
android:endColor="#FF483D8B"
android:angle="42"
android:type="radial"
android:gradientRadius="40"/>
</shape>
android:shape
属性值有:"rectangle" | "oval" | "line" | "ring"
rectangle是矩形,也是默认值
oval是椭圆形,如果设置为这个属性corners就没有效果了
line是直线,ring是环形
1) solid :填充颜色
只有一个属性android:color,表示需要填充的颜色,默认应该是是全透明色。
即跟
android:color="@android:color/transparent"
效果是一样的。
2) stroke:边框、描边
<stroke
android:width="8dp"
android:color="@android:color/white"/>
android:width 表示实线框的宽度
android:color 表示边框的颜色
另外,还有两个属性代表虚线框:
android:dashWidth="5dp" 表示虚线框的宽度
android:dashGap="3dp" 表示虚线框的虚线之间的间隔
这两个属性任意一个属性为0时,代表的是实线
3) padding:间隔
<padding
android:left="8dp"
android:right="8dp"
android:top="8dp"
android:bottom="8dp"/>
相当于背景沿着上、下、左、右分别拉伸一定的间隔,也就是说中间的填充分别向四个方向拉伸一定间隔
4) corners:圆角
<corners
android:radius="5dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
android:radius:radius是半径的意思,可以想象一个半径越小,这个弧度应该越小,半径越大这个弧度也是越大的,但是肯定有个上限,超过了就看不到效果了android:topLeftRadius:左上角弧度
android:topRightRadius:右上角弧度
android:bottomLeftRadius:左下角弧度,网上的很多资料应该是sdk版本比较旧,这个属性当时是右下角,但是我在4.4上是左下角,google应该是修正过来了
android:bottomRightRadius:右下角
5) gradient:渐变
<gradient
android:startColor="#FFFFD700"
android:endColor="#FF483D8B"
android:angle="45"
android:type="sweep"/>
gradient的属性稍微繁琐一点。
android:startColor:渐变的起始色,如果只设置这个不设置endColor,会向底图颜色渐变
android:endColor: 渐变的终止色,同样如果没有设置startColor,会以底图颜色为起点色
android:angle: 配合渐变,这个值为0-315,设置必须为45的倍数,不然会报错的。0就是左边,45就是左下角,逆时针依次增加
android:type: 渐变方式,属性有linear、sweep、radial,默认是linear即线性渐变。angle属性也只有在type是linear的时候有效,其他的时候是没有效果的。radial的意思是从中心渐变,如果type的属性值为radial的时候,必须要设置渐变半径即另一个属性gradientRadius。
android:gradientRadius:渐变半径配合type为radial使用
另外还有三个属性需要看一下source code解释,暂时未知
android:centerX
android:centerY
android:useLevel