shape详解

示例一:

 1 <shape>
 2             <!-- 实心 -->
 3             <solid android:color="#ff9d77"/>
 4             <!-- 渐变 -->
 5             <gradient
 6                 android:startColor="#ff8c00"
 7                 android:endColor="#FFFFFF"
 8                 android:angle="270" />
 9             <!-- 描边 -->
10             <stroke
11                 android:width="2dp"
12                 android:color="#dcdcdc" />
13             <!-- 圆角 -->
14             <corners
15                 android:radius="2dp" />
16             <padding
17                 android:left="10dp"
18                 android:top="10dp"
19                 android:right="10dp"
20                 android:bottom="10dp" />
21  </shape>

属性说明:

1、  solid:填充的意思
android:color指定填充的颜色

2、  gradient:渐变
android:startColor
android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"

3、  stroke:描边
android:width="2dp"
描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

4、  corners:圆角
android:radius
为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners 
        android:topRightRadius="20dp"   
右上角
        android:bottomLeftRadius="20dp"   
右下角
        android:topLeftRadius="1dp"   
左上角
        android:bottomRightRadius="0dp"   
左下角
 />
这里有个地方需要注意,
bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

5、  padding:间隔
这个就不用多说了,XML布局文件中经常用到。

示例二
Selector作为Button的背景,分别定义了按钮的一般状态、获得焦点状态和按下时的状态,代码如下:

 1 main.xml:
 2 <Button
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:text="TestShapeButton"
 6     android:background="@drawable/button_selector"
 7     />
 8 
 9 button_selector.xml:
10 <?xml version="1.0" encoding="utf-8"?>
11 <selector
12     xmlns:android="http://schemas.android.com/apk/res/android">
13     <item android:state_pressed="true" >
14         <shape>
15             <!-- 渐变 -->
16             <gradient
17                 android:startColor="#ff8c00"
18                 android:endColor="#FFFFFF"
19                 android:type="radial"
20                 android:gradientRadius="50" />
21             <!-- 描边 -->
22             <stroke
23                 android:width="2dp"
24                 android:color="#dcdcdc"
25                 android:dashWidth="5dp" 
26                 android:dashGap="3dp" />
27             <!-- 圆角 -->
28             <corners
29                 android:radius="2dp" />
30             <padding
31                 android:left="10dp"
32                 android:top="10dp"
33                 android:right="10dp"
34                 android:bottom="10dp" />
35         </shape>
36     </item>
37 
38     <item android:state_focused="true" >
39         <shape>
40             <gradient
41                 android:startColor="#ffc2b7"
42                 android:endColor="#ffc2b7"
43                 android:angle="270" />
44             <stroke
45                 android:width="2dp"
46                 android:color="#dcdcdc" />
47             <corners
48                 android:radius="2dp" />
49             <padding
50                 android:left="10dp"
51                 android:top="10dp"
52                 android:right="10dp"
53                 android:bottom="10dp" />
54         </shape>
55     </item>
56 
57     <item>       
58         <shape>
59             <solid android:color="#ff9d77"/>
60             <stroke
61                 android:width="2dp"
62                 android:color="#fad3cf" />
63             <corners 
64                 android:topRightRadius="5dp"
65                 android:bottomLeftRadius="5dp"
66                 android:topLeftRadius="0dp"
67                 android:bottomRightRadius="0dp"
68             />
69             <padding
70                 android:left="10dp"
71                 android:top="10dp"
72                 android:right="10dp"
73                 android:bottom="10dp" />
74         </shape>
75     </item>
76 </selector>
原文地址:https://www.cnblogs.com/easypass/p/2988310.html