背景
在Android
开发中,使用shape
标签可以很方便的帮助我们自定义绘制简单的形状,一般可用于绘制控件的背景,如按钮或文本框背景,也经常用于设置布局的背景,其用法不难但是功能作用却很强大。相对于png
图片来说,使用shape
可以减少安装包的大小,而且能够更好的适配不同的手机。
XML定义
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="15dp"
android:innerRadiusRatio="2"
android:shape="ring"
android:thickness="10dp"
android:thicknessRatio="2"
android:useLevel="false">
<!-- 填充 -->
<solid android:color="@color/red" />
<!-- 圆角 -->
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:radius="10dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<!-- 渐变 -->
<gradient
android:angle="45"
android:centerColor="@color/red"
android:centerX="0"
android:centerY="0"
android:endColor="@color/black"
android:gradientRadius="90"
android:startColor="@color/blue"
android:type="radial"
android:useLevel="false" />
<!-- 间隔 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 大小 -->
<size
android:width="75dp"
android:height="45dp" />
<!-- 描边 -->
<stroke
android:width="1dp"
android:color="@color/blue"
android:dashGap="2dp"
android:dashWidth="0dp" />
</shape>
属性
solid:填充
1. android:color
:填充的颜色。
corners:圆角
1. android:radius
:设置四个角的圆角半径。
2. android:bottomLeftRadius
:设置左下角的圆角半径。
3. android:bottomRightRadius
:设置右下角的圆角半径 。
4. android:topLeftRadius
:设置左上角的圆角半径 。
5. android:topRightRadius
:设置右上角的圆角半径 。
注:如果同时设置五个属性,则radius
属性无效。
gradient:渐变
1. android:startColor
:起始渐变颜色。
2. android:centerColor
:中间渐变颜色。
3. android:endColor
:结束渐变颜色。
4. android:centerX
:渐变中心X
的相对位置,放射渐变时才有效,范围为0~1
,默认为0.5
,表示在正中间。
5. android:centerY
:渐变中心Y
的相对位置,放射渐变时才有效,范围为0~1
,默认为0.5
,表示在正中间。
6. android:type
:渐变类型,有三种:radial
放射渐变;linear
线性渐变,默认为此渐变模式;sweep
扫描性渐变。
7. android:angle
:渐变角度,必须是45
的整数倍(包括0
),0
表示从左到右,90
表示从下到上。
8. android:gradientRadius
:渐变的半径,只有当渐变类型为radial
时才能使用。
9. android:useLevel
:设为false
时才有渐变效果,使用LevelListDrawable
时要设置为true
。
padding:内间距
1. android:top
:距离顶部内间距。
2. android:bottom
:距离底部内间距。
3. android:left
:距离左边内间距。
4. android:right
:距离右边内间距。
size:大小
1. android:width
:宽度。
2. android:height
:高度。
stroke:描边
1. android:width
:设置边框的宽度 。
2. android:color
:设置边框的颜色。
3. android:dashGap
:设置虚线的间隔宽度。
4. android:dashWidth
:设置虚线的宽度。
注:dashWidth
和dashGap
属性只要其中一个设置为0dp
,则边框为实线边框。
shape:形状,默认为矩形
1. android:shape="ring"
:环形。
2. android:shape="line"
:线性形。
3. android:shape="oval"
:椭圆形。
4. android:shape="rectangle"
:矩形。
如果设置为android:shape="ring"
时还可以设置一下属性:
1. android:innerRadius
:尺寸,内环的半径。
2. android:innerRadiusRatio
:浮点型,以环的宽度比率来表示内环的半径,默认为3
,表示内环半径为环的宽度除以3
,该值会被innerRadius
覆盖。
3. android:thickness
:尺寸,环的厚度。
4. android:thicknessRatio
:浮点型,以环的宽度比率来表示环的厚度,默认为9
,表示环的厚度为环的宽度除以9
,该值会被thickness
覆盖。
5. android:useLevel
:boolean
值,一般设置为false
,否则可能环形无法显示,只有作为LevelListDrawable
使用时才设为true
。