2 *另一种封装方式 3 * 4 */ 5 import android.content.Context;
6 import android.view.Gravity;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.ImageView;
11 import android.widget.LinearLayout;
12 import android.widget.TextView;
13 import android.widget.Toast;
14 15 import com.letv.recorder.R;
16 17 /** 18 * Created by juyanming on 16/7/4. 19 */ 20 public class ToastUtils {
21 22 /** 23 * 描述:系统Toast 24 * @param context 应用上下文 25 * @param str 消息框提示的文本内容 26 */ 27 public void t_default(Context context,String str){
28 Toast.makeText(context, str,
29 Toast.LENGTH_SHORT).show();
30 }
31 32 /** 33 * 描述:自定义图片Toast 34 * @param context 应用上下文 35 * @param str 消息框提示的文本内容 36 * @param imgId 图片id 37 */ 38 public void t_img(Context context,String str,
int imgId){
39 Toast toast = Toast.makeText(context,
40 "带图片的Toast", Toast.LENGTH_LONG);
41 toast.setGravity(Gravity.CENTER, 0, 0);
42 LinearLayout toastView = (LinearLayout) toast.getView();
43 ImageView imageCodeProject =
new ImageView(context);
44 imageCodeProject.setImageResource(imgId);
45 toastView.addView(imageCodeProject, 0);
46 toast.show();
47 }
48 49 /** 50 * 51 * @param context 应用上下文 52 * @param str 消息框提示的文本内容 53 * @param custom 布局 54 * @param llToast viewId 55 * @param icon 图片id 56 * @param tvImageToast 视图id 57 * @param tvTitleToast 58 * @param tvTextToast 59 */ 60 public void t_complete(Context context,String str,
int custom,
int llToast,
int icon,
int tvImageToast,
int tvTitleToast,
int tvTextToast){
61 LayoutInflater inflater = LayoutInflater.from(context);
62 View layout = inflater.inflate(custom,
63 (ViewGroup) findViewById(R.id.llToast));
64 ImageView image = (ImageView) layout
65 .findViewById(tvImageToast);
66 image.setImageResource(icon);
67 TextView title = (TextView) layout.findViewById(tvTitleToast);
68 title.setText("Attention");
69 TextView text = (TextView) layout.findViewById(tvTextToast);
70 text.setText("完全自定义Toast");
71 Toast toast =
new Toast(context);
72 toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
73 toast.setDuration(Toast.LENGTH_LONG);
74 toast.setView(layout);
75 toast.show();
76 }
77 }