Android自定义Button按钮显示样式 转http://my.oschina.net/amigos/blog/63009

2019年11月09日 阅读数:121
这篇文章主要向大家介绍Android自定义Button按钮显示样式 转http://my.oschina.net/amigos/blog/63009,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

首先写一个定义Button样式的XML文件:android

新建Android XML文件,类型选Drawable,根结点选selector,文件名就buton_style吧布局

 

?
1
2
3
4
5
6
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     < item android:state_pressed = "true" android:drawable = "@drawable/play_press" />
     < item android:state_focused = "true" android:drawable = "@drawable/play_press" />
     < item android:drawable = "@drawable/play" />
</ selector >

 

我这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。字体

 

接下来只要在布局时写Button控件时应用到Button的Background属性便可。spa

 

?
1
2
3
4
5
< Button android:id = "@+id/button1"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:background = "@drawable/button_style" >
</ Button >

 

再加上一种自定义样式方法,上面的是用图片,其实咱们能够直接经过定义xml文件来实现不一样的样式: 
在上面的源代码基础上,只须要修改button_style文件,一样三种状态分开定义
.net

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     < item android:state_pressed = "true" >
         < shape >
             < gradient android:startColor = "#0d76e1" android:endColor = "#0d76e1"
                 android:angle = "270" />
             < stroke android:width = "1dip" android:color = "#f403c9" />
             < corners android:radius = "2dp" />
             < padding android:left = "10dp" android:top = "10dp"
                 android:right = "10dp" android:bottom = "10dp" />
         </ shape >
     </ item >
   
     < item android:state_focused = "true" >
         < shape >
             < gradient android:startColor = "#ffc2b7" android:endColor = "#ffc2b7"
                 android:angle = "270" />
             < stroke android:width = "1dip" android:color = "#f403c9" />
             < corners android:radius = "2dp" />
             < padding android:left = "10dp" android:top = "10dp"
                 android:right = "10dp" android:bottom = "10dp" />
         </ shape >
     </ item >
   
     < item >
         < shape >
             < gradient android:startColor = "#000000" android:endColor = "#ffffff"
                 android:angle = "180" />
             < stroke android:width = "1dip" android:color = "#f403c9" />
             < corners android:radius = "5dip" />
             < padding android:left = "10dp" android:top = "10dp"
                 android:right = "10dp" android:bottom = "10dp" />
         </ shape >
     </ item >
</ selector >

 

gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推) 
stroke 边框 width 边框宽度,color 边框颜色 
corners 圆角 radius 半径,0为直角 
padding text值的相对位置
code

 

 3:实现渐变色效果:orm

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<? xml version = "1.0" encoding = "UTF-8" ?>
 
< shape xmlns:android = "http://schemas.android.com/apk/res/android" >
 
<!-- 颜色渐变效果 -->
 
< gradient
 
android:startColor = "#00FF00"
 
android:endColor = "#C0C0C0"
 
android:paddingTop = "5dp"
 
android:angle = "0" />
 
<!-- 按钮四个角的平滑度 -->
 
< corners android:radius = "15dp" />
 
</ shape >

 

4:字体颜色配置文件:xml

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? xml version = "1.0" encoding = "utf-8" ?>
 
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
 
< item android:state_selected = "true" android:color = "#FFF" />
 
< item android:state_focused = "true" android:color = "#FFF" />
 
< item android:state_pressed = "true" android:color = "#FFF" />
 
< item android:color = "#000" />
 
</ selector >
//用法;
 
< Button
 
android:layout_width = "fill_parent"
 
android:layout_height = "wrap_content"
 
android:background = "@drawable/btn_ctrllist"
 
android:textSize = "30dip"
 
android:id = "@+id/button_cal"
 
android:text = "电话控制" />

 

固然除了使用 drawable 这样的图片外今天谈下自定义图形 shape 的方法,对于 Button 控件Android 上支持如下几种属性 shape、gradient、stroke、corners 等。 

咱们就以目前系统的 Button 的 selector 为例说下:
blog

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
< shape >
 
< gradient
 
android:startColor = "#ff8c00"
 
android:endColor = "#FFFFFF"
 
android:angle = "270" />
 
< stroke
 
android:width = "2dp"
 
android:color = "#dcdcdc" />
 
< corners
 
android:radius = "2dp" />
 
< padding
 
android:left = "10dp"
 
android:top = "10dp"
 
android:right = "10dp"
 
android:bottom = "10dp" />
 
</ shape >

 

对于上面,这条 shape 的定义,分别为渐变,在 gradient 中 startColor 属性为开始的颜色,endColor 为渐变结束的颜色,下面的 angle 是角度。接下来是 stroke 能够理解为边缘,corners为拐角这里 radius 属性为半径,最后是相对位置属性 padding。事件

 

 

对于一个 Button 完整的定义能够为:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<? xml version = "1.0" encoding = "utf-8" ?>
 
< selector xmlns:android = "http://www.norkoo.com" >
 
< item android:state_pressed = "true" >
 
< shape >
 
< gradient
 
android:startColor = "#ff8c00"
 
android:endColor = "#FFFFFF"
 
android:angle = "270" />
 
< stroke
 
android:width = "2dp"
 
android:color = "#dcdcdc" />
 
< corners
 
android:radius = "2dp" />
 
< padding
 
android:left = "10dp"
 
android:top = "10dp"
 
android:right = "10dp"
 
android:bottom = "10dp" />
 
</ shape >
 
</ item >
 
< item android:state_focused = "true" >
 
< shape >
 
< gradient
 
android:startColor = "#ffc2b7"
 
android:endColor = "#ffc2b7"
 
android:angle = "270" />
 
< stroke
 
android:width = "2dp"
 
android:color = "#dcdcdc" />
 
< corners
 
android:radius = "2dp" />
 
< padding
 
android:left = "10dp"
 
android:top = "10dp"
 
android:right = "10dp"
 
android:bottom = "10dp" />
 
</ shape >
 
</ item >
 
< item >
 
< shape >
 
< gradient
 
android:startColor = "#ff9d77"
 
android:endColor = "#ff9d77"
 
android:angle = "270" />
 
< stroke
 
android:width = "2dp"
 
android:color = "#fad3cf" />
 
< corners
 
android:radius = "2dp" />
 
< padding
 
android:left = "10dp"
 
android:top = "10dp"
 
android:right = "10dp"
 
android:bottom = "10dp" />