package com.example.liujianglong.testapp;

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

/**
 * Created by liujianglong on 2017/9/11.
 */

public class ToastUtil {
    private static Toast mToast;
    private static int mGravity = -1;

    private ToastUtil() {
        /*prevent form initial*/
    }

    public static void configGravity(int gravity) {
        mGravity = gravity;
    }

    private static LinearLayout getView(Context context) {
        Context appContext = context.getApplicationContext();
        LinearLayout layout = new LinearLayout(appContext);
        TextView textView = new TextView(appContext);
        ImageView imageView = new ImageView(appContext);
        layout.addView(imageView);
        layout.addView(textView);
        return layout;
    }

    /**
     * set text ,duration and drawable
     */
    public static void showToastWithDrawable(Context context, String text, int drawableId, int duration) {
        LinearLayout toastView = getView(context);
        TextView textViewCenter = (TextView) toastView.getChildAt(1);
        textViewCenter.setText(text);
        if (drawableId > 0) {
            ImageView image = (ImageView) toastView.getChildAt(0);
            image.setBackgroundResource(drawableId);
            image.setVisibility(View.VISIBLE);
        }
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = new Toast(context.getApplicationContext());

        if (mGravity >= 0) {
            mToast.setGravity(mGravity, 0, 0);
        }
        mToast.setDuration(duration);
        mToast.setView(toastView);
        mToast.show();
    }

    /**
     * set text, default LENGTH_SHORT time
     */
    public static void showToast(Context context, String text) {
        showToastWithDrawable(context, text, -1, Toast.LENGTH_SHORT);
    }

    /**
     * set string resource ID, default LENGTH_SHORT time
     */
    public static void showToast(Context context, int stringResId) {
        showToast(context, context.getString(stringResId));
    }
}

@@ -746,109 +746,713 @@
     
-TextView textView = new TextView(appContext);%0D%0A        ImageView imageView = new ImageView(appContext
+int padding = (int) DisplayUtil.dip2px(12);%0D%0A        layout.setPadding(padding, padding, padding, padding);%0D%0A        layout.setBackgroundResource(R.drawable.toast_bg);%0D%0A        layout.setOrientation(LinearLayout.HORIZONTAL);%0D%0A        layout.setGravity(Gravity.CENTER_VERTICAL);%0D%0A        TextView textView = new TextView(appContext);%0D%0A        ImageView imageView = new ImageView(appContext);%0D%0A        imageView.setVisibility(View.GONE);%0D%0A        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);%0D%0A        lp.setMargins(0, 0, padding, 0);%0D%0A        imageView.setLayoutParams(lp);%0D%0A        textView.setTextColor(Color.WHITE
 );%0D%0A
@@ -2420,32 +2420,388 @@
 ow();%0D%0A    %7D%0D%0A%0D%0A
+    public static void showDrawable(Context context, int resId, String text) %7B%0D%0A        showToastWithDrawable(context, text, resId, Toast.LENGTH_SHORT);%0D%0A    %7D%0D%0A%0D%0A    public static void showDrawable(Context context, int resId, int stringResId) %7B%0D%0A        showToastWithDrawable(context, context.getString(stringResId), resId, Toast.LENGTH_SHORT);%0D%0A    %7D%0D%0A%0D%0A
     /**%0D%0A     * 

update util