先看两张效果图版本4.4
版本5.0+
看完这两张图后,要实现以上按钮的selector,颜色角度不同的情况下会分别创建不同的selector和shape,,现在只用一个ButtonBgUi就可以达到以上的效果,,再也不用重复的创建selector,老规矩,先看使用方法:
step1:Add it in your root build.gradle at the end of repositories:
1
allprojects {
2
repositories {
3
...
4
maven { url 'https://jitpack.io' }
5
}
6
}
Step 2. Add the dependency
1
dependencies {
2
compile 'com.github.ithedan:CustomViewBgUi:v1.0'
3
}
布局中的使用方法
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
xmlns:app="http://schemas.android.com/apk/res-auto"
4
xmlns:tools="http://schemas.android.com/tools"
5
android:layout_width="match_parent"
6
android:layout_height="match_parent"
7
android:background="#f7f7f8"
8
android:orientation="vertical"
9
tools:context="com.came.customviewbgui.MainActivity">
10
11
<LinearLayout
12
android:layout_width="match_parent"
13
android:layout_height="wrap_content"
14
android:layout_marginLeft="20dp"
15
android:layout_marginRight="20dp"
16
android:layout_marginTop="20dp">
17
18
<com.came.viewbguilib.ButtonBgUi
19
android:layout_width="0dp"
20
android:layout_height="50dp"
21
android:layout_marginRight="10dp"
22
android:layout_weight="1"
23
android:text="充值"
24
android:textSize="18sp"
25
android:textColor="#ffffff"
26
android:clickable="true"
27
app:defaultColor="#52adff"
28
app:topRightRadius="25dp"
29
app:bottomRightRadius="25dp"
30
/>
31
32
<com.came.viewbguilib.ButtonBgUi
33
android:layout_width="0dp"
34
android:layout_height="50dp"
35
android:layout_marginLeft="10dp"
36
android:layout_weight="1"
37
android:text="提现"
38
android:textSize="18sp"
39
android:textColor="#ffffff"
40
android:clickable="true"
41
app:defaultColor="#52adff"
42
app:topLeftRadius="25dp"
43
app:bottomLeftRadius="25dp"
44
/>
45
</LinearLayout>
46
47
<com.came.viewbguilib.ButtonBgUi
48
android:layout_width="match_parent"
49
android:layout_height="50dp"
50
android:text="登陆"
51
android:textSize="18sp"
52
android:textColor="#ffffff"
53
android:clickable="true"
54
android:layout_marginLeft="20dp"
55
android:layout_marginRight="20dp"
56
android:layout_marginTop="20dp"
57
app:defaultColor="#f7964f"
58
app:raoundRadius="5dp"
59
/>
60
61
<com.came.viewbguilib.ButtonBgUi
62
android:layout_width="match_parent"
63
android:layout_height="50dp"
64
android:text="注册"
65
android:textSize="18sp"
66
android:textColor="#ffffff"
67
android:clickable="true"
68
android:layout_marginLeft="20dp"
69
android:layout_marginRight="20dp"
70
android:layout_marginTop="20dp"
71
app:defaultColor="#c95fdc"
72
app:pressedColor="#995ae9"
73
app:raoundRadius="5dp"
74
/>
75
76
<LinearLayout
77
android:layout_width="match_parent"
78
android:layout_height="wrap_content"
79
android:layout_marginLeft="20dp"
80
android:layout_marginRight="20dp"
81
android:layout_marginTop="20dp">
82
83
<com.came.viewbguilib.ButtonBgUi
84
android:layout_width="0dp"
85
android:layout_height="50dp"
86
android:layout_marginRight="10dp"
87
android:layout_weight="1"
88
android:text="登陆"
89
android:textSize="18sp"
90
android:textColor="#ffffff"
91
android:clickable="true"
92
app:strokeColor="#f7964f"
93
app:strokeWidth="0.5dp"
94
app:defaultColor="#58cca0"
95
app:pressedColor="#2b9d4b"
96
app:topRightRadius="10dp"
97
app:topLeftRadius="5dp"
98
app:bottomLeftRadius="10dp"
99
app:bottomRightRadius="5dp"
100
101
/>
102
103
<com.came.viewbguilib.ButtonBgUi
104
android:layout_width="0dp"
105
android:layout_height="50dp"
106
android:layout_marginLeft="10dp"
107
android:layout_weight="1"
108
android:text="注册"
109
android:textSize="18sp"
110
android:textColor="#ffffff"
111
android:clickable="true"
112
app:defaultColor="#58cca0"
113
app:pressedColor="#2b9d4b"
114
app:strokeColor="#f7964f"
115
app:strokeWidth="0.5dp"
116
app:topRightRadius="5dp"
117
app:topLeftRadius="10dp"
118
app:bottomLeftRadius="5dp"
119
app:bottomRightRadius="10dp"
120
/>
121
</LinearLayout>
122
123
<com.came.viewbguilib.ButtonBgUi
124
android:layout_width="match_parent"
125
android:layout_height="50dp"
126
android:text="提交"
127
android:textSize="18sp"
128
android:textColor="#f7964f"
129
android:clickable="true"
130
android:layout_marginLeft="20dp"
131
android:layout_marginRight="20dp"
132
android:layout_marginTop="20dp"
133
app:raoundRadius="5dp"
134
app:strokeColor="#f7964f"
135
app:strokeWidth="2dp"
136
app:selectStrokeColor="#1fde94"
137
android:background="#ffffff"
138
/>
139
140
<LinearLayout
141
android:layout_width="match_parent"
142
android:layout_height="wrap_content"
143
android:layout_marginLeft="20dp"
144
android:layout_marginRight="20dp"
145
android:layout_marginTop="20dp">
146
147
<com.came.viewbguilib.ButtonBgUi
148
android:layout_width="wrap_content"
149
android:layout_height="wrap_content"
150
android:text="美妆"
151
android:textSize="18sp"
152
android:textColor="#f7964f"
153
android:clickable="true"
154
android:layout_marginLeft="30dp"
155
android:padding="5dp"
156
app:strokeWidth="2dp"
157
app:strokeColor="#4be1c3"
158
/>
159
160
<com.came.viewbguilib.ButtonBgUi
161
android:layout_width="wrap_content"
162
android:layout_height="wrap_content"
163
android:layout_marginLeft="20dp"
164
android:text="漫画"
165
android:textSize="18sp"
166
android:textColor="#f7964f"
167
android:clickable="true"
168
android:padding="5dp"
169
app:strokeWidth="2dp"
170
app:strokeColor="#4be1c3"
171
app:raoundRadius="10dp"
172
/>
173
<com.came.viewbguilib.ButtonBgUi
174
android:layout_width="wrap_content"
175
android:layout_height="wrap_content"
176
android:text="韩国电影"
177
android:textSize="18sp"
178
android:textColor="#f7964f"
179
android:clickable="true"
180
android:layout_marginLeft="20dp"
181
android:padding="5dp"
182
app:strokeWidth="2dp"
183
app:strokeColor="#4be1c3"
184
app:raoundRadius="20dp"
185
app:defaultColor="#00000000"
186
app:pressedColor="#fa734e"
187
/>
188
<com.came.viewbguilib.ButtonBgUi
189
android:layout_width="wrap_content"
190
android:layout_height="wrap_content"
191
android:text="财经"
192
android:textSize="18sp"
193
android:textColor="#f7964f"
194
android:clickable="true"
195
android:layout_marginLeft="20dp"
196
android:padding="5dp"
197
app:strokeWidth="2dp"
198
app:strokeColor="#4be1c3"
199
app:topRightRadius="5dp"
200
app:topLeftRadius="10dp"
201
app:bottomLeftRadius="5dp"
202
app:bottomRightRadius="10dp"
203
/>
204
205
</LinearLayout>
206
207
</LinearLayout>
自定义属性说明
注意事项》》》》》》》》
1、如果不需要背景选择器,只需要不用设置defaultColor
1
<com.came.viewbguilib.ButtonBgUi
2
android:layout_width="wrap_content"
3
android:layout_height="wrap_content"
4
android:text="美妆"
5
android:textSize="18sp"
6
android:textColor="#f7964f"
7
android:clickable="true"
8
android:layout_marginLeft="30dp"
9
android:padding="5dp"
10
app:strokeWidth="2dp"
11
app:strokeColor="#4be1c3"
12
/>
2、如果不设置pressedColor选中的颜色,会根据defaultColor默认的颜色判断选中的颜色是变亮或变暗
1
<com.came.viewbguilib.ButtonBgUi
2
android:layout_width="0dp"
3
android:layout_height="50dp"
4
android:layout_marginRight="10dp"
5
android:layout_weight="1"
6
android:text="充值"
7
android:textSize="18sp"
8
android:textColor="#ffffff"
9
android:clickable="true"
10
app:defaultColor="#52adff"
11
app:topRightRadius="25dp"
12
app:bottomRightRadius="25dp"
13
/>
3、如果不想在5.0+设备上有水波纹效果添加app:isRipple="false"
如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~ 源码地址
属性 | 说明 |
---|---|
strokeColor | 默认边框的颜色 |
selectStrokeColor | 选中边框的颜色 |
pressedColor | 选中的背景色 |
defaultColor | 默认的背景色 |
isRipple | 是否设置水波纹效果 |
parameter | 颜色变亮或变浅参数默认0.2 |
strokeWidth | 边框的宽度 |
raoundRadius | 圆角 |
topLeftRadius | 左上角圆角 |
topRightRadius | 右上角圆角 |
bottomLeftRadius | 左下角圆角 |
bottomRightRadius | 右下角圆角 |