forked from cnLGMing/SnackBarUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnackBarUtils.java
205 lines (184 loc) · 6.1 KB
/
SnackBarUtils.java
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
package com.hakkalgm.snackbar;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @创建者 Hakka_LGM
* @创建时间 2016-6-3 16:35
* @描述 对SnackBar进行封装
*/
public class SnackBarUtils {
public static final int INFO = 1;
public static final int CONFIRM = 2;
public static final int WARNING = 3;
public static final int ALERT = 4;
public static int red = 0xfff44336;
public static int green = 0xff4caf50;
public static int blue = 0xff2195f3;
public static int orange = 0xffffc107;
/**
* Snackbar:自定义颜色的短显示
*
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar shortSnackbar(View view, String message, int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
setSnackBarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* Snackbar:自定义颜色的长显示
*
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar longSnackbar(View view, String message, int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
setSnackBarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* 短显示Snackbar,可选预设类型
*
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar shortSnackbar(View view, String message, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
switchType(snackbar, type);
return snackbar;
}
/**
* 长显示Snackbar,可选预设类型
*
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar longSnackbar(View view, String message, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
switchType(snackbar, type);
return snackbar;
}
/**
* 自定义时常显示Snackbar,自定义颜色
*
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar IndefiniteSnackbar(View view, String message, int duration,
int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar
.make(view, message, Snackbar.LENGTH_INDEFINITE)
.setDuration(duration);
setSnackBarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* 自定义时常显示Snackbar,可选预设类型
*
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar IndefiniteSnackbar(View view, String message, int duration, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).setDuration(duration);
switchType(snackbar, type);
return snackbar;
}
/**
* 设置Snackbar背景颜色
*
* @param snackbar
* @param backgroundColor
*/
public static void setSnackBarColor(Snackbar snackbar, int backgroundColor) {
View view = snackbar.getView();
if (view != null) {
view.setBackgroundColor(backgroundColor);
}
}
/**
* 设置SnackBar消息的颜色
*
* @param snackbar
* @param messageColor
*/
public static void setSnackBarMsgColor(Snackbar snackbar, int messageColor) {
View view = snackbar.getView();
if (view != null) {
((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
}
}
/**
* 设置Snackbar文字和背景颜色
*
* @param snackbar
* @param messageColor
* @param backgroundColor
*/
public static void setSnackBarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
View view = snackbar.getView();
if (view != null) {
view.setBackgroundColor(backgroundColor);
((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
}
}
/**
* 为SnackBar添加布局
*
* @param snackbar SnackBar实例
* @param layoutId 布局文件
* @param index 位置
*/
public static void addView(Snackbar snackbar, int layoutId, int index) {
/** 获取snackbar的View(其实就是SnackbarLayout) */
View snackbarview = snackbar.getView();
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbarview;
View add_view = LayoutInflater.from(snackbarview.getContext())
.inflate(layoutId, null); //加载布局文件新建View
// 设置新建布局参数
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 设置新建布局在Snackbar内垂直居中显示
p.gravity = Gravity.CENTER_VERTICAL;
// 将新建布局添加进snackbarLayout相应位置
snackbarLayout.addView(add_view, index, p);
}
//选择预设类型
private static void switchType(Snackbar snackbar, int type) {
switch (type) {
case INFO:
setSnackBarColor(snackbar, blue);
break;
case CONFIRM:
setSnackBarColor(snackbar, green);
break;
case WARNING:
setSnackBarColor(snackbar, orange);
break;
case ALERT:
setSnackBarColor(snackbar, Color.YELLOW, red);
break;
}
}
}