应用场景:
在Android中常常会使用shape来定义控件的一些显示属性,例如背景颜色、背景圆角、背景渐变等效果。
使用方式:
第一步:在工程的res目录下新建drawable文件夹(若已存在无需新建),在drawable文件夹下新建Android XML File文件shape00.xml。
第二步:shape00.xml中填写文件内容,相关各个属性的作用与可选内容介绍如下。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!-- 圆角 -->
<corners
android:radius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp">
<!--
android:radius 设置角的弧度,值越大角越圆
-->
</corners>
<!-- 渐变 -->
<gradient
android:startColor="#123456"
android:centerColor="#234567"
android:endColor="#345678"
android:useLevel="true"
android:angle="0"
android:type="linear"
android:centerX="1%"
android:centerY="3%"
>
<!--
当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效
android:angle 渐变角度(PS:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往
上。angle必须为45的整数倍)
android:type 渐变类型(取值:linear、radial、sweep)
linear 线性渐变,这是默认设置
radial 放射性渐变,以开始色为中心。
sweep 扫描线式的渐变。
android:centerColor 渐变中间颜色,即开始颜色与结束颜色之间的颜色
android:useLevel 如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。false有渐变色
android:gradientRadius 渐变色半径.当 android:type="radial" 时才使用。单独使用 android:type="radial"会报
错。
android:centerX 渐变中心X点坐标的相对位置
android:centerY 渐变中心Y点坐标的相对位置
-->
</gradient>
<!-- 间隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp">
</padding>
<!-- 大小 -->
<size
android:width="134dp"
android:height="36dp">
</size>
<!-- 填充 -->
<solid
android:color="#567846">
</solid>
<!-- 描边 -->
<stroke
android:width="2dp"
android:color="@android:color/black"
android:dashWidth="2dp"
android:dashGap="12dp">
<!--
dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框
android:width="20dp" 设置边边的宽度
android:color="@android:color/black" 设置边边的颜色
android:dashWidth="2dp" 设置虚线的宽度
android:dashGap="20dp" 设置虚线的间隔宽度
-->
</stroke>
</shape>
android:background="@drawable/shape00"
页面效果:效果截图 仅供演示