/
ToastUtil.java
55 lines (44 loc) · 1.52 KB
/
ToastUtil.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
package com.loneyang.ble2nfc.utils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import com.loneyang.ble2nfc.R;
/**
* Created by user on 2017/5/19.
*/
public class ToastUtil {
private Context context;
private Toast toast;
public ToastUtil(Context context) {
this.context = context.getApplicationContext();
toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
public boolean showToast(String str) {
if (toast.getView().getParent() != null) {
toast.cancel();
}
toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
toast.show();
return true;
}
public boolean showToastWithImg(String str, Drawable imgRes) {
if (toast.getView().getParent() != null) {
toast.cancel();
}
toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(16);
textView.setText(str);
textView.setPadding(80, 0, 80, 0);
imgRes.setBounds(0, 0, imgRes.getMinimumWidth(), imgRes.getMinimumHeight());
textView.setCompoundDrawables(imgRes, null, null, null);
textView.setBackground(context.getResources().getDrawable(R.drawable.shape_round_toast));
toast.setView(textView);
toast.show();
return false;
}
}