floatingactionbutton项目在github上的主页:
floatingactionbutton使用简单,而且可以自定义颜色、大小、背景图片
项目构造:
下面是demo的代码(主要见sample):https://github.com/futuresimple/android-floating-action-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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:fab= "http://schemas.android.com/apk/res-auto"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:background= "@color/background" >
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/pink_icon"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentbottom= "true"
android:layout_centerhorizontal= "true"
android:layout_marginbottom= "dp"
fab:fab_colornormal= "@color/pink"
fab:fab_colorpressed= "@color/pink_pressed"
fab:fab_icon= "@drawable/ic_fab_star" />
<textview
style= "@style/menu_labels_style"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@id/pink_icon"
android:layout_centerhorizontal= "true"
android:layout_marginbottom= "dp"
android:text= "text below button" />
<com.getbase.floatingactionbutton.addfloatingactionbutton
android:id= "@+id/semi_transparent"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@id/pink_icon"
android:layout_centerhorizontal= "true"
android:layout_marginbottom= "dp"
fab:fab_colornormal= "@color/blue_semi_transparent"
fab:fab_colorpressed= "@color/blue_semi_transparent_pressed"
fab:fab_plusiconcolor= "@color/white" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/setter"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@id/semi_transparent"
android:layout_centerhorizontal= "true"
android:layout_marginbottom= "dp" />
<com.getbase.floatingactionbutton.addfloatingactionbutton
android:id= "@+id/normal_plus"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentbottom= "true"
android:layout_alignparentleft= "true"
android:layout_alignparentstart= "true"
android:layout_marginbottom= "dp"
android:layout_marginleft= "dp"
android:layout_marginstart= "dp"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_plusiconcolor= "@color/half_black" />
<com.getbase.floatingactionbutton.floatingactionsmenu
android:id= "@+id/right_labels"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@id/normal_plus"
android:layout_alignparentleft= "true"
android:layout_alignparentstart= "true"
android:layout_marginleft= "dp"
android:layout_marginstart= "dp"
fab:fab_addbuttoncolornormal= "@color/white"
fab:fab_addbuttoncolorpressed= "@color/white_pressed"
fab:fab_addbuttonplusiconcolor= "@color/half_black"
fab:fab_addbuttonsize= "mini"
fab:fab_labelstyle= "@style/menu_labels_style"
fab:fab_labelsposition= "right" >
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_title= "label on the right" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_size= "mini"
fab:fab_title= "another one on the right" />
</com.getbase.floatingactionbutton.floatingactionsmenu>
<com.getbase.floatingactionbutton.floatingactionsmenu
android:id= "@+id/multiple_actions"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentbottom= "true"
android:layout_alignparentend= "true"
android:layout_alignparentright= "true"
android:layout_marginbottom= "dp"
android:layout_marginend= "dp"
android:layout_marginright= "dp"
fab:fab_addbuttoncolornormal= "@color/white"
fab:fab_addbuttoncolorpressed= "@color/white_pressed"
fab:fab_addbuttonplusiconcolor= "@color/half_black"
fab:fab_labelstyle= "@style/menu_labels_style" >
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/action_a"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_title= "action a" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/action_b"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_title= "action with a very long name that won\'t fit on the screen" />
</com.getbase.floatingactionbutton.floatingactionsmenu>
<com.getbase.floatingactionbutton.floatingactionsmenu
android:id= "@+id/multiple_actions_down"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentend= "true"
android:layout_alignparentright= "true"
android:layout_alignparenttop= "true"
android:layout_marginend= "dp"
android:layout_marginright= "dp"
android:layout_margintop= "dp"
fab:fab_addbuttoncolornormal= "@color/white"
fab:fab_addbuttoncolorpressed= "@color/white_pressed"
fab:fab_addbuttonplusiconcolor= "@color/half_black"
fab:fab_addbuttonsize= "mini"
fab:fab_expanddirection= "down"
fab:fab_labelstyle= "@style/menu_labels_style" >
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_size= "mini" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/button_remove"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_title= "click to remove" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/button_gone"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/action_enable"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_title= "set bottom menu enabled/disabled" />
</com.getbase.floatingactionbutton.floatingactionsmenu>
<com.getbase.floatingactionbutton.floatingactionsmenu
android:id= "@+id/multiple_actions_left"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparenttop= "true"
android:layout_marginend= "dp"
android:layout_marginright= "dp"
android:layout_margintop= "dp"
android:layout_toleftof= "@+id/multiple_actions_down"
android:layout_tostartof= "@+id/multiple_actions_down"
fab:fab_addbuttoncolornormal= "@color/white"
fab:fab_addbuttoncolorpressed= "@color/white_pressed"
fab:fab_addbuttonplusiconcolor= "@color/half_black"
fab:fab_addbuttonsize= "mini"
fab:fab_addbuttonstrokevisible= "false"
fab:fab_expanddirection= "left" >
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_size= "mini" />
<com.getbase.floatingactionbutton.floatingactionbutton
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:visibility= "gone"
fab:fab_colornormal= "@color/white"
fab:fab_colorpressed= "@color/white_pressed"
fab:fab_size= "mini" />
</com.getbase.floatingactionbutton.floatingactionsmenu>
<com.getbase.floatingactionbutton.floatingactionbutton
android:id= "@+id/setter_drawable"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@id/setter"
android:layout_centerhorizontal= "true" />
</relativelayout>
java代码:
package com.getbase.floatingactionbutton.sample;
import com.getbase.floatingactionbutton.floatingactionbutton;
import com.getbase.floatingactionbutton.floatingactionsmenu;
import android.app.activity;
import android.graphics.drawable.shapedrawable;
import android.graphics.drawable.shapes.ovalshape;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.toast;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
findviewbyid(r.id.pink_icon).setonclicklistener( new onclicklistener() {
@override
public void onclick(view v) {
toast.maketext(mainactivity. this , "clicked pink floating action button" , toast.length_short).show();
}
});
floatingactionbutton button = (floatingactionbutton) findviewbyid(r.id.setter);
button.setsize(floatingactionbutton.size_mini);
button.setcolornormalresid(r.color.pink);
button.setcolorpressedresid(r.color.pink_pressed);
button.seticon(r.drawable.ic_fab_star);
button.setstrokevisible( false );
final view actionb = findviewbyid(r.id.action_b);
floatingactionbutton actionc = new floatingactionbutton(getbasecontext());
actionc.settitle( "hide/show action above" );
actionc.setonclicklistener( new onclicklistener() {
@override
public void onclick(view v) {
actionb.setvisibility(actionb.getvisibility() == view.gone ? view.visible : view.gone);
}
});
final floatingactionsmenu menumultipleactions = (floatingactionsmenu) findviewbyid(r.id.multiple_actions);
menumultipleactions.addbutton(actionc);
final floatingactionbutton removeaction = (floatingactionbutton) findviewbyid(r.id.button_remove);
removeaction.setonclicklistener( new onclicklistener() {
@override
public void onclick(view v) {
((floatingactionsmenu) findviewbyid(r.id.multiple_actions_down)).removebutton(removeaction);
}
});
shapedrawable drawable = new shapedrawable( new ovalshape());
drawable.getpaint().setcolor(getresources().getcolor(r.color.white));
((floatingactionbutton) findviewbyid(r.id.setter_drawable)).seticondrawable(drawable);
final floatingactionbutton actiona = (floatingactionbutton) findviewbyid(r.id.action_a);
actiona.setonclicklistener( new onclicklistener() {
@override
public void onclick(view view) {
actiona.settitle( "action a clicked" );
}
});
// test that fams containing fabs with visibility gone do not cause crashes
findviewbyid(r.id.button_gone).setvisibility(view.gone);
final floatingactionbutton actionenable = (floatingactionbutton) findviewbyid(r.id.action_enable);
actionenable.setonclicklistener( new onclicklistener() {
@override
public void onclick(view view) {
menumultipleactions.setenabled(!menumultipleactions.isenabled());
}
});
floatingactionsmenu rightlabels = (floatingactionsmenu) findviewbyid(r.id.right_labels);
floatingactionbutton addedonce = new floatingactionbutton( this );
addedonce.settitle( "added once" );
rightlabels.addbutton(addedonce);
floatingactionbutton addedtwice = new floatingactionbutton( this );
addedtwice.settitle( "added twice" );
rightlabels.addbutton(addedtwice);
rightlabels.removebutton(addedtwice);
rightlabels.addbutton(addedtwice);
}
}
|
colors:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<color name= "black_semi_transparent" >#b2000000</color>
<color name= "background" >#e5e5e5</color>
<color name= "half_black" ># 808080 </color>
<color name= "white" >#fafafa</color>
<color name= "white_pressed" >#f1f1f1</color>
<color name= "pink" >#e91e63</color>
<color name= "pink_pressed" >#ec407a</color>
<color name= "blue_semi_transparent" >#805677fc</color>
<color name= "blue_semi_transparent_pressed" >#80738ffe</color>
</resources>
|