package com.danding.yang800.Util;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ToastUtil {

    private static Toast toast = null;

    /**
     * 普通Toast
     *
     * @param context
     * @param text
     */
    public static void show(Context context, String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    /**
     * 设置Toast提示消息在屏幕上的位置
     *
     * @param context
     * @param text
     */
    public static void TextToast(Context context, String text) {
        // 创建一个Toast提示消息
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        // 设置Toast提示消息在屏幕上的位置
        toast.setGravity(Gravity.CENTER, 0, 0);
        // 显示消息
        toast.show();
    }

    /**
     * 带图片消息提示
     *
     * @param context
     * @param ImageResourceId
     * @param text
     */
    public static void ImageToast(Context context, int ImageResourceId,
                                  String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        LinearLayout toastView = (LinearLayout) toast.getView();
        ImageView imageCodeProject = new ImageView(context);
        imageCodeProject.setImageResource(ImageResourceId);
        toastView.addView(imageCodeProject, 0);
        toast.show();
    }

}
