Android自定义Button按钮显示样式 转http://my.oschina.net/amigos/blog/63009
首先写一个定义Button样式的XML文件:
新建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属性即可。
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文件,同样三种状态分开定义
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值的相对位置
3:实现渐变色效果:
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:字体颜色配置文件:
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 为例说下:
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" /> </ shape > </ item > </ selector > |
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:color = "hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_active=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </ selector > Elements: < selector > |
android:state_pressed
Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。android:state_focused
Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦 Button);“false”表示非聚焦状态使用。
android:state_selected Boolean。“true”表示选中状态使用(例如 Tab 打开);“false”表示非选中状态使用。
android:state_checkable Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)android:state_checked Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。android:state_enabled Boolean。“true”表示可用状态使用(能接收触摸/点击事件):“false”表示不可用状态使用。android:window_focused Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如 Notification 栏拉下或对话框显示)。