上两篇讲了布局控件一些常用的属性介绍。今天讲下shape。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@android:color/black"/>
<stroke android:color="@color/colorAccent" android:width="1px"/>
</shape>
shape有几种形状:
android:shape=“rectangle|line|oval|ring”// 矩形、线条、椭圆、圆环
shape有几种属性:
corners:圆角半径
gradient:颜色渐变
padding:内边距
size:尺寸大小
solid:填充颜色
stroke:边框
下面将一一介绍各个属性都有哪些参数可以设置:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"// 必须为false,否则无法显示
android:innerRadiusRatio="2.5"// 比率=圆环宽度/圆环内径
android:innerRadius="20dp"// 圆环内径
android:thickness="10dp"// 圆环厚度
android:thicknessRatio="2.5"// 比率=圆环宽度/圆环厚度
android:tint="@color/colorAccent"// shape颜色,solid颜色会失效
>
<corners
android:bottomLeftRadius="10dp"// 坐下角
android:bottomRightRadius="10dp"// 右下角
android:topLeftRadius="10dp"// 左上角
android:topRightRadius="10dp"/>// 右上角
<corners
android:radius="10dp" />
这两段代码效果是一样的,都是表示圆角半径为10dp。
<gradient
android:angle="45"// 渐变角度,只能是45的倍数
android:startColor=""// 渐变的初始颜色
android:endColor=""// 渐变的终止颜色
android:centerColor=""// 渐变中心的颜色
android:centerX="50%"// 渐变中点的x坐标:float:0~1
android:centerY="50%"// 渐变中点的y坐标:float:0~1
android:gradientRadius="0dp"// 渐变半径,type=radial
android:type="sweep|radial|linear"// 渐变效果:扫描、溅射、线性
android:useLevel="true|false"/>// 暂未用过
<padding // 内边距
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
<size // 设置图形固定大小
android:height="10dp"
android:width="200dp"/>
<solid // 设置填充图形的颜色
android:color="@android:color/black"/>
<stroke // 设置线条边框
android:color="@android:color/black"
android:width="1px"// 边框宽度
android:dashGap="10dp"// 虚线间隙
android:dashWidth="10dp"/> // 虚线宽度
注意:当shape="line"时,View的android:layout_height(size的height)值要比stroke的width大,否则不会显示;当画虚线时,需要关闭硬件加速,可再View的布局中添加android:layerType=“software”,当然还有其他方法。