首先,告诉你一个好消息,这个知识点非常的简单!!!
一、在 studio中res 包下的drawable中建立一个shape的文件,系统会给你一个默认的方形然后你就可以开始设置你需要的效果。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--填充背景色 -->
<solid android:color="#ffff00" /> //solid 指的是背景颜色的设置
<!--描边 他需要 2个参数,颜色 第二参数 宽度 -->
<stroke
android:width="2dp" //width 指的是边框的宽度
android:color="#ff00ff" /> //color指的是边框的颜色
<!-- 设置弧度 Radius设置所有角的弧度 --> //radius 指的就是角的弧度
<corners //方形存在四个角,我们可以同时设置四个角,也可以分开设置
android:bottomLeftRadius="20dp" //左下角
android:bottomRightRadius="20dp" //右下角
android:topLeftRadius="20dp" //左上角
android:topRightRadius="20dp" /> //右上角
//同时设置:<corners
android:Radius="20dp" /> //四个角同时设置
<!-- 渐变色 startColor 启示颜色 endColor最终颜色 type类型(如果类型为radial(径向渐变,一个圆心以半径的方式渐变),必须加上 android:gradientRadius="**")-->
<gradient
android:startColor="#ff0000" //开始颜色
android:endColor="#ffffff" //结束颜色
android:type="linear" /> //水平渐变 从左到右 type 指的是渐变的模式
<!--内容与边框的间距-->
<padding
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp" />
<!--如果设置了渐变色 那么背景颜色将不显示 -->
</shape>
<gradient>
attributes:
- Integer,代表渐变颜色的角度, 0 is left to right, 90 is bottom to top. 必须是45的整数倍.
-
默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。
默认情况下,从左到右:
-
xml代码:<gradient
android:startColor="#000000"
android:endColor="#ffffff"
/>
angle=270,从上到下 :
-
xml代码:<gradient
android:startColor="#000000"
android:endColor="#ffffff"
android:angle="270"
/>
- Color. 颜色渐变的开始颜色,如angle=270中的 android:startColor="#000000"
- Color. 颜色渐变的结束颜色,如angle=270中的 android:endColor="#ffffff"
- Color. 颜色渐变的中间颜色,主要用于多彩。
-
-
<gradient
android:startColor="#000000"
android:endColor="#ffffff"
android:centerColor="#ff0000"
/>
- Float.(0 - 1.0) 相对X的渐变位置。
- Float.(0 - 1.0) 相对Y的渐变位置。
- 这两个属性只有在type不为linear情况下起作用。
-
Float. 渐变颜色的半径,单位应该是像素点. 需要
android:type="radial"
.
如果android:type="radial",没有设置android:gradientRadius,将会报错,error inflating class.
-
Value Description "linear"
线性渐变.可以理解为 y=kx+b. "radial"
A radial gradient.圆形渐变,起始颜色从cenralX,centralY点开始。 "sweep"
A sweeping line gradient.
android:angle
android:startColor
android:endColor
android:centerColor
android:centerX
android:centerY
android:gradientRadius
xml代码: <gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="30"
android:type="radial"
/>
加入Android:centerColor属性
<gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerColor="#000000"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="30"
android:type="radial"
/>
android:type
centerX="0.2" centerX="0.2"
xml代码: <gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.2"
android:centerX="0.2"
android:gradientRadius="30"
android:type="radial"
/>
type="sweep":
xml代码:
<gradientandroid:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.5"
android:centerY="0.5"
android:type="sweep"
/>