有时候我们会需要自定义drawable,用来当一些文字的背景图片,使用方式为在drawable文件夹下新建xml文件
1.自定义drawable中的图形可以为oval(椭圆),ring(圆环),rectangle(长方形),line(线段)
2.首先我们看下rectangle
-
<pre name="code" class="html"> "1.0" encoding="utf-8" xml version=
-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
-
android:shape= "rectangle" >
-
-
<corners
-
android:radius= "30dip"/>
-
<solid android:color="#000000"/>
-
-
<stroke android:color="#ffffff"
-
android:width= "4dp"
-
android:dashWidth= "10dp"
-
android:dashGap= "3dp"/>
-
<size
-
android:width= "200dp"
-
android:height= "400dp"/>
-
-
<gradient
-
android:startColor= "#ff00ff"
-
android:endColor= "#000000"
-
android:type= "linear"
-
android:centerX= "0.5"
-
android:centerColor= "#ffffff"
-
android:centerY= "0.5"
-
android:angle= "45"/>
-
-
</shape>
解释一下上面的属性:1.coners:设置的是四个角的半径,设置之后就圆角矩形了
2.solid : 填充图形的颜色
3.stroke:设置的是边框,其中color是边框的颜色,width是边框的宽度
dashWidth表示横线的宽度,dashGap:表示为隔开的距离,可以看到上面
上传的图片边框是一段一段,就是使用上面俩个属性的效果
4.设置的是图形的大小
5.gradient:这个是设置渐变色的,startColor为开始的颜色,centerColor为中间的颜色
endColor为结束时的颜色,centerX,centerY是设置中间颜色从哪开始取值范围为
(0~1),angle为角度,这个你们可以自己去设置为0,45,90,去体会一下
补充:
solid: 设置形状填充的颜色,只有android:color一个属性
- android:color 填充的颜色
padding: 设置内容与形状边界的内间距,可分别设置左右上下的距离
- android:left 左内间距
- android:right 右内间距
- android:top 上内间距
- android:bottom 下内间距
gradient: 设置形状的渐变颜色,可以是线性渐变、辐射渐变、扫描性渐变
- android:type 渐变的类型
- linear 线性渐变,默认的渐变类型
- radial 放射渐变,设置该项时,android:gradientRadius也必须设置
- sweep 扫描性渐变
- android:startColor 渐变开始的颜色
- android:endColor 渐变结束的颜色
- android:centerColor 渐变中间的颜色
- android:angle 渐变的角度,线性渐变时才有效,必须是45的倍数,0表示从左到右,90表示从下到上
- android:centerX 渐变中心的相对X坐标,放射渐变时才有效,在0.0到1.0之间,默认为0.5,表示在正中间
- android:centerY 渐变中心的相对X坐标,放射渐变时才有效,在0.0到1.0之间,默认为0.5,表示在正中间
- android:gradientRadius 渐变的半径,只有渐变类型为radial时才使用
- android:useLevel 如果为true,则可在LevelListDrawable中使用
- android:type 渐变的类型
corners: 设置圆角,只适用于rectangle类型,可分别设置四个角不同半径的圆角,当设置的圆角半径很大时,比如200dp,就可变成弧形边了
- android:radius 圆角半径,会被下面每个特定的圆角属性重写
- android:topLeftRadius 左上角的半径
- android:topRightRadius 右上角的半径
- android:bottomLeftRadius 左下角的半径
- android:bottomRightRadius 右下角的半径
stroke: 设置描边,可描成实线或虚线。
- android:color 描边的颜色
- android:width 描边的宽度
- android:dashWidth 设置虚线时的横线长度
- android:dashGap 设置虚线时的横线之间的距离
上面这些是我看英文文档中的,建议大家直接去看英文文档,链接:英文文档
2.ring(圆环)
-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
-
android:shape= "ring"
-
android:useLevel= "false"
-
android:innerRadius= "50dp"
-
android:thickness= "20dp"
-
>
-
-
<solid
-
android:color= "#ff00ff"/>
-
<size
-
android:width= "150dp"
-
android:height= "150dp"/>
-
<stroke
-
android:width= "3dp"
-
android:color= "#000000"
-
/>
-
</shape>
ring这个我感觉用到的不多,如果需要用到大小圆就另说了,其他用oval来代替
大部分属性在前面介绍过来,介绍几个前面没有介绍过的
1.useLevel:false (默认值为true,这有把useLevel设置为false,圆环才能显示出来,具体为什么没看懂文档)
2.thickNess:为外圆环的厚度
3.innerRadius为内圆环的半径,如果不设置上面这俩个值会有默认值
3.oval(椭圆,圆)
oval 用法与rectangle差不多,就不细讲了
-
"1.0" encoding="utf-8" xml version=
-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
-
android:shape= "oval"
-
>
-
-
<corners
-
android:radius= "30dip"/>
-
<solid android:color="#ff00ff"/>
-
-
<stroke android:color="#ffffff"
-
android:width= "4dp"
-
android:dashWidth= "10dp"
-
android:dashGap= "3dp"/>
-
<size
-
android:width= "200dp"
-
android:height= "200dp"/>
-
-
-
</shape>
有时候我们会用到镂空的圆角矩形,就是只有一个框框,实现效果如下:
-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
-
android:shape= "rectangle"
-
>
-
-
<size
-
android:width= "200dp"
-
android:height= "100dp"/>
-
<stroke android:color="@android:color/darker_gray"
-
android:width= "2dp"/>
-
<corners android:radius="50dp"
-
/>
-
</shape>
不要设置填充的颜色(solid)属性即可