15
15
import android .support .annotation .StringRes ;
16
16
import android .support .v4 .view .ViewCompat ;
17
17
import android .support .v4 .widget .TextViewCompat ;
18
- import android .view .Gravity ;
19
18
import android .view .LayoutInflater ;
20
19
import android .view .View ;
21
20
import android .widget .TextView ;
@@ -35,12 +34,12 @@ public final class ToastUtils {
35
34
private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
36
35
37
36
private static Toast sToast ;
38
- private static int gravity = Gravity . CENTER_HORIZONTAL | Gravity . BOTTOM ;
39
- private static int xOffset = 0 ;
40
- private static int yOffset = ( int ) ( 64 * Utils . getApp (). getResources (). getDisplayMetrics (). density + 0.5 ) ;
41
- private static int bgColor = COLOR_DEFAULT ;
42
- private static int bgResource = -1 ;
43
- private static int msgColor = COLOR_DEFAULT ;
37
+ private static int sGravity = - 1 ;
38
+ private static int sXOffset = - 1 ;
39
+ private static int sYOffset = - 1 ;
40
+ private static int sBgColor = COLOR_DEFAULT ;
41
+ private static int sBgResource = -1 ;
42
+ private static int sMsgColor = COLOR_DEFAULT ;
44
43
45
44
private ToastUtils () {
46
45
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -54,9 +53,9 @@ private ToastUtils() {
54
53
* @param yOffset y 偏移
55
54
*/
56
55
public static void setGravity (final int gravity , final int xOffset , final int yOffset ) {
57
- ToastUtils . gravity = gravity ;
58
- ToastUtils . xOffset = xOffset ;
59
- ToastUtils . yOffset = yOffset ;
56
+ sGravity = gravity ;
57
+ sXOffset = xOffset ;
58
+ sYOffset = yOffset ;
60
59
}
61
60
62
61
/**
@@ -65,7 +64,7 @@ public static void setGravity(final int gravity, final int xOffset, final int yO
65
64
* @param backgroundColor 背景色
66
65
*/
67
66
public static void setBgColor (@ ColorInt final int backgroundColor ) {
68
- ToastUtils . bgColor = backgroundColor ;
67
+ sBgColor = backgroundColor ;
69
68
}
70
69
71
70
/**
@@ -74,7 +73,7 @@ public static void setBgColor(@ColorInt final int backgroundColor) {
74
73
* @param bgResource 背景资源
75
74
*/
76
75
public static void setBgResource (@ DrawableRes final int bgResource ) {
77
- ToastUtils . bgResource = bgResource ;
76
+ sBgResource = bgResource ;
78
77
}
79
78
80
79
/**
@@ -83,7 +82,7 @@ public static void setBgResource(@DrawableRes final int bgResource) {
83
82
* @param msgColor 颜色
84
83
*/
85
84
public static void setMsgColor (@ ColorInt final int msgColor ) {
86
- ToastUtils . msgColor = msgColor ;
85
+ sMsgColor = msgColor ;
87
86
}
88
87
89
88
/**
@@ -208,11 +207,15 @@ private static void show(final CharSequence text, final int duration) {
208
207
public void run () {
209
208
cancel ();
210
209
sToast = Toast .makeText (Utils .getApp (), text , duration );
211
- // solve the font of toast
210
+ //t solve the font of toast
212
211
TextView tvMessage = sToast .getView ().findViewById (android .R .id .message );
213
212
TextViewCompat .setTextAppearance (tvMessage , android .R .style .TextAppearance );
214
- tvMessage .setTextColor (msgColor );
215
- sToast .setGravity (gravity , xOffset , yOffset );
213
+ if (sMsgColor != COLOR_DEFAULT ) {
214
+ tvMessage .setTextColor (sMsgColor );
215
+ }
216
+ if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
217
+ sToast .setGravity (sGravity , sXOffset , sYOffset );
218
+ }
216
219
setBg (tvMessage );
217
220
sToast .show ();
218
221
}
@@ -227,7 +230,9 @@ public void run() {
227
230
sToast = new Toast (Utils .getApp ());
228
231
sToast .setView (view );
229
232
sToast .setDuration (duration );
230
- sToast .setGravity (gravity , xOffset , yOffset );
233
+ if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
234
+ sToast .setGravity (sGravity , sXOffset , sYOffset );
235
+ }
231
236
setBg ();
232
237
sToast .show ();
233
238
}
@@ -236,37 +241,37 @@ public void run() {
236
241
237
242
private static void setBg () {
238
243
View toastView = sToast .getView ();
239
- if (bgResource != -1 ) {
240
- toastView .setBackgroundResource (bgResource );
241
- } else if (bgColor != COLOR_DEFAULT ) {
244
+ if (sBgResource != -1 ) {
245
+ toastView .setBackgroundResource (sBgResource );
246
+ } else if (sBgColor != COLOR_DEFAULT ) {
242
247
Drawable background = toastView .getBackground ();
243
248
if (background != null ) {
244
249
background .setColorFilter (
245
- new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN )
250
+ new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN )
246
251
);
247
252
} else {
248
- ViewCompat .setBackground (toastView , new ColorDrawable (bgColor ));
253
+ ViewCompat .setBackground (toastView , new ColorDrawable (sBgColor ));
249
254
}
250
255
}
251
256
}
252
257
253
258
private static void setBg (final TextView tvMsg ) {
254
259
View toastView = sToast .getView ();
255
- if (bgResource != -1 ) {
256
- toastView .setBackgroundResource (bgResource );
260
+ if (sBgResource != -1 ) {
261
+ toastView .setBackgroundResource (sBgResource );
257
262
tvMsg .setBackgroundColor (Color .TRANSPARENT );
258
- } else if (bgColor != COLOR_DEFAULT ) {
263
+ } else if (sBgColor != COLOR_DEFAULT ) {
259
264
Drawable tvBg = toastView .getBackground ();
260
265
Drawable msgBg = tvMsg .getBackground ();
261
266
if (tvBg != null && msgBg != null ) {
262
- tvBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
267
+ tvBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
263
268
tvMsg .setBackgroundColor (Color .TRANSPARENT );
264
269
} else if (tvBg != null ) {
265
- tvBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
270
+ tvBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
266
271
} else if (msgBg != null ) {
267
- msgBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
272
+ msgBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
268
273
} else {
269
- toastView .setBackgroundColor (bgColor );
274
+ toastView .setBackgroundColor (sBgColor );
270
275
}
271
276
}
272
277
}
0 commit comments