今天主要介绍shape的xml文件。
先看下主要介绍的效果,按钮为例:
1:在drawable文件下创建一个xml文件 例如:shabtn.xml
第一个我们可以看下面的文本注释
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!--类型为线-->
<stroke android:width="2dp"/>
<!--边框宽度2个dp-->
</shape>
类型矩形 圆角15 bt2.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--矩形-->
<corners android:radius="15dp"/>
<!--圆角15-->
<solid android:color="@android:color/darker_gray"/>
<!--加了个填充 奶奶灰色的-->
</shape>
类型矩形 圆角15 加边框 宽度 1dp bt3.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--矩形-->
<corners android:radius="15dp"/>
<!--圆角15-->
<stroke android:width="2dp"
android:dashGap="20dp"
android:dashWidth="2dp"/>
<!--线宽 2dp 间隔线间隔 2dp 间隔线宽 20dp-->
<solid android:color="@android:color/darker_gray"/>
<!--填充 奶奶灰-->
</shape>
类型矩形 扫描性渐变 bt5.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="sweep"
android:startColor="@android:color/holo_green_light"
android:endColor="@android:color/holo_blue_light"
/>
<!--类型为扫描性渐变-->
<!--开始颜色 绿色-->
<!--结束颜色 蓝色-->
</shape>
类型矩形 线性渐变 bt6.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="linear"
android:angle="45"
android:startColor="@android:color/holo_orange_light"
android:endColor="@android:color/white"
/>
<!--渐变角只在线性渐变有效 需为45的倍数-->
<!--线性渐变 渐变角45-->
<!--开始颜色 》 结束颜色-->
</shape>
类型矩形 放射性渐变 bt7.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:centerX="0.5"
android:centerY="0.5"
android:startColor="@android:color/holo_red_dark"
android:centerColor="@color/colorPrimary"
android:endColor="@android:color/darker_gray"
android:gradientRadius="200dp"
/>
<!--渐变坐标只在放射性渐变有效 坐标在[0,1]的闭区间 渐变半径也只在放射性渐变有效-->
<!--类型放射性渐变-->
<!--渐变中心角-->
<!-- 开始颜色 中间颜色 结束颜色 渐变半径 -->
</shape>
2:完整的其他属性见下:
<?xml version="1.0" encoding="UTF-8"?>
<!--创建shape标签 shape属性 rectangle 矩形、方形 ring 环形 oval 椭圆形 line 线-->
<shape android:shape="line"
android:innerRadius="5dp" //shape时可用 内环半径
android:tint="@color/White" //着色
android:innerRadiusRatio="8" //内环厚度比 默认3 会被覆盖
android:useLevel="true" //一般为false true时不显示但可在draw able中使用
android:visible="true" //显示
android:thickness="5dp" //环的厚度
android:thicknessRatio="8" //厚度比 宽度/厚度 默认9
xmlns:android="http://schemas.android.com/apk/res/android">
<!--描边-->
<stroke
android:width="5dp" //线的宽度
android:dashWidth="1dp" //虚线宽度
android:dashGap="3dp" //虚线间隔
android:color="@color/Black" //线颜色
/>
<!--大小-->
<size
android:width="50dp" //宽度
android:height="50dp" //高度
/>
<!--内边距-->
<padding
android:bottom="50dp" //底部边距
android:left="50dp" //左边
android:right="50dp" //右边
android:top="50dp" //上面
/>
<!--渐变-->
<gradient
android:type="sweep" //linear 线性渐变 radial 放射性渐变 sweep 扫描性渐变
android:angle="45" //渐变角度 需要为45的倍数 0度时从左往右 逆时针 在为线性渐变时有效
android:startColor="@color/Black" //-渐变开始的颜色 到 渐变中间的颜色 到结束颜色
android:centerColor="@color/Gray"
android:endColor="@color/White"
android:centerX="0.5" //渐变中心X坐标 [0,1] 在type为放射性渐变radial时有效
android:centerY="0.5" //渐变中心Y坐标 [0,1] 在type为放射性渐变radial时有效
android:gradientRadius="2dp" //渐变半径 放射性渐变有效
/>
<!--填充-->
<solid
android:color="@color/White" //填充颜色
/>
<!--圆角-->
<corners
android:radius="60dp" //圆角半径 此值有效时其他无效
android:bottomLeftRadius="5dp" //左下角 圆角半径
android:bottomRightRadius="5dp" //右下角 圆角半径
android:topLeftRadius="5dp" //左上角 圆角半径
android:topRightRadius="5dp" //右上角 圆角半径
/>