首页 > 技术文章 > Android 自定义Toast

huanyi0723 2016-01-08 15:53 原文

自定义Toast

其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多

上图上代码

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button show1 = (Button)findViewById(R.id.show1);
    show1.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        SuperToast superToast = new SuperToast(MainActivity.this);
        
        //设置属性
        superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
        superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
        superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
        superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
        superToast.setIcon(R.drawable.icon_load, SuperToast.IconPosition.LEFT); //图片显示
        
        superToast.setText("登录中...");
        superToast.setGravity(Gravity.CENTER, 0, 0);
        
        superToast.show();
        
      }
    });
    
    Button show2 = (Button)findViewById(R.id.show2);
    show2.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        SuperToast superToast = new SuperToast(MainActivity.this);
        
        //设置属性
        superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
        superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
        superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
        superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
        
        superToast.setText("MeiID或密码错误,请重新输入");
        superToast.setGravity(Gravity.CENTER, 0, 0);
        
        superToast.show();
        
      }
    });
    
    Button show3 = (Button)findViewById(R.id.show3);
    show3.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        SuperToast superToast = new SuperToast(MainActivity.this);
        
        //设置属性
        superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
        superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
        superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
        superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
        superToast.setIcon(R.drawable.icon_ok, SuperToast.IconPosition.LEFT); //图片显示
        
        superToast.setText("重置密码成功");
        superToast.setGravity(Gravity.CENTER, 0, 0);
        
        superToast.show();
        
      }
    });
    
    Button show4 = (Button)findViewById(R.id.show4);
    show4.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        SuperToast superToast = new SuperToast(MainActivity.this);
        
        //设置属性
        superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
        superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
        superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
        superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
        
        superToast.setText("验证码已发送到您的手机,10分钟内\n有效,请注意查收短信");
        superToast.setGravity(Gravity.CENTER, 0, 0);
        
        superToast.show();
        
      }
    });
    
    
    
  }

}
public class ManagerSuperToast extends Handler {

    @SuppressWarnings("UnusedDeclaration")
    private static final String TAG = "ManagerSuperToast";

    /* Potential messages for the handler to send **/
    private static final class Messages {

        /* Hexadecimal numbers that represent acronyms for the operation **/
        private static final int DISPLAY_SUPERTOAST = 0x445354;
        private static final int ADD_SUPERTOAST = 0x415354;
        private static final int REMOVE_SUPERTOAST = 0x525354;

    }

    private static ManagerSuperToast mManagerSuperToast;

    private final Queue<SuperToast> mQueue;

    /* Private method to create a new list if the manager is being initialized */
    private ManagerSuperToast() {

        mQueue = new LinkedBlockingQueue<SuperToast>();

    }

    /* Singleton method to ensure all SuperToasts are passed through the same manager */
    protected static synchronized ManagerSuperToast getInstance() {

        if (mManagerSuperToast != null) {

            return mManagerSuperToast;

        } else {

            mManagerSuperToast = new ManagerSuperToast();

            return mManagerSuperToast;

        }

    }

    /* Add SuperToast to queue and try to show it */
    protected void add(SuperToast superToast) {

        /* Add SuperToast to queue and try to show it */
        mQueue.add(superToast);
        this.showNextSuperToast();

    }

    /* Shows the next SuperToast in the list */
    private void showNextSuperToast() {

        if (mQueue.isEmpty()) {

            /* There is no SuperToast to display next */

            return;

        }

        /* Get next SuperToast in the queue */
        final SuperToast superToast = mQueue.peek();

        /* Show SuperToast if none are showing (not sure why this works but it does) */
        if (!superToast.isShowing()) {

            final Message message = obtainMessage(Messages.ADD_SUPERTOAST);
            message.obj = superToast;
            sendMessage(message);

        } else {

            sendMessageDelayed(superToast, Messages.DISPLAY_SUPERTOAST,
                    getDuration(superToast));

        }

    }

    /* Show/dismiss a SuperToast after a specific duration */
    private void sendMessageDelayed(SuperToast superToast, final int messageId, final long delay) {

        Message message = obtainMessage(messageId);
        message.obj = superToast;
        sendMessageDelayed(message, delay);

    }

    /* Get duration and add one second to compensate for show/hide animations */
    private long getDuration(SuperToast superToast) {

        long duration = superToast.getDuration();
        duration += 1000;

        return duration;

    }

    @Override
    public void handleMessage(Message message) {

        final SuperToast superToast = (SuperToast)
                message.obj;

        switch (message.what) {

            case Messages.DISPLAY_SUPERTOAST:

                showNextSuperToast();

                break;

            case Messages.ADD_SUPERTOAST:

                displaySuperToast(superToast);

                break;

            case Messages.REMOVE_SUPERTOAST:

                removeSuperToast(superToast);

                break;

            default: {

                super.handleMessage(message);

                break;

            }

        }

    }

    /* Displays a SuperToast */
    private void displaySuperToast(SuperToast superToast) {

        if (superToast.isShowing()) {

            /* If the SuperToast is already showing do not show again */

            return;

        }

        final WindowManager windowManager = superToast
                .getWindowManager();

        final View toastView = superToast.getView();

        final WindowManager.LayoutParams params = superToast
                .getWindowManagerParams();

        if(windowManager != null) {

            windowManager.addView(toastView, params);

        }

        sendMessageDelayed(superToast, Messages.REMOVE_SUPERTOAST,
                superToast.getDuration() + 500);

    }

    /* Hide and remove the SuperToast */
    protected void removeSuperToast(SuperToast superToast) {

        final WindowManager windowManager = superToast
                .getWindowManager();

        final View toastView = superToast.getView();

        if (windowManager != null) {

            mQueue.poll();

            windowManager.removeView(toastView);

            sendMessageDelayed(superToast,
                    Messages.DISPLAY_SUPERTOAST, 500);

            if(superToast.getOnDismissListener() != null) {

                superToast.getOnDismissListener().onDismiss(superToast.getView());

            }

        }

    }

    /* Cancels/removes all showing pending SuperToasts */
    protected void cancelAllSuperToasts() {

        removeMessages(Messages.ADD_SUPERTOAST);
        removeMessages(Messages.DISPLAY_SUPERTOAST);
        removeMessages(Messages.REMOVE_SUPERTOAST);

        for (SuperToast superToast : mQueue) {

            if (superToast.isShowing()) {

                superToast.getWindowManager().removeView(
                        superToast.getView());

            }

        }

        mQueue.clear();

    }

}
public class Style {

    public static final int BLACK = 0;
    public static final int BLUE = 1;
    public static final int GRAY = 2;
    public static final int GREEN = 3;
    public static final int ORANGE = 4;
    public static final int PURPLE = 5;
    public static final int RED = 6;
    public static final int WHITE = 7;

    public SuperToast.Animations animations = SuperToast.Animations.FADE;
    public int background = getBackground(GRAY);
    public int typefaceStyle = Typeface.NORMAL;
    public int textColor = Color.WHITE;
    public int dividerColor = Color.WHITE;
    public int buttonTextColor = Color.LTGRAY;

    /**
     * Returns a preset style.
     *
     * @param styleType {@link Style}
     *
     * @return {@link Style}
     */
    public static Style getStyle(int styleType) {

        final Style style = new Style();

        switch (styleType) {

            case BLACK:

                style.textColor = Color.WHITE;
                style.background = getBackground(BLACK);
                style.dividerColor = Color.WHITE;
                return style;

            case WHITE:

                style.textColor = Color.DKGRAY;
                style.background = getBackground(WHITE);
                style.dividerColor = Color.DKGRAY;
                style.buttonTextColor = Color.GRAY;
                return style;

            case GRAY:

                style.textColor = Color.WHITE;
                style.background = getBackground(GRAY);
                style.dividerColor = Color.WHITE;
                style.buttonTextColor = Color.GRAY;
                return style;

            case PURPLE:

                style.textColor = Color.WHITE;
                style.background = getBackground(PURPLE);
                style.dividerColor = Color.WHITE;
                return style;

            case RED:

                style.textColor = Color.WHITE;
                style.background = getBackground(RED);
                style.dividerColor = Color.WHITE;
                return style;

            case ORANGE:

                style.textColor = Color.WHITE;
                style.background = getBackground(ORANGE);
                style.dividerColor = Color.WHITE;
                return style;

            case BLUE:

                style.textColor = Color.WHITE;
                style.background = getBackground(BLUE);
                style.dividerColor = Color.WHITE;
                return style;

            case GREEN:

                style.textColor = Color.WHITE;
                style.background = getBackground(GREEN);
                style.dividerColor = Color.WHITE;
                return style;

            default:

                style.textColor = Color.WHITE;
                style.background = getBackground(GRAY);
                style.dividerColor = Color.WHITE;
                return style;

        }

    }

    /**
     * Returns a preset style with specified animations.
     *
     * @param styleType {@link Style}
     * @param animations {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
     *
     * @return {@link Style}
     */
    public static Style getStyle(int styleType, SuperToast.Animations animations) {

        final Style style = new Style();
        style.animations = animations;

        switch (styleType) {

            case BLACK:

                style.textColor = Color.WHITE;
                style.background = getBackground(BLACK);
                style.dividerColor = Color.WHITE;
                return style;

            case WHITE:

                style.textColor = Color.DKGRAY;
                style.background = getBackground(WHITE);
                style.dividerColor = Color.DKGRAY;
                style.buttonTextColor = Color.GRAY;
                return style;

            case GRAY:

                style.textColor = Color.WHITE;
                style.background = getBackground(GRAY);
                style.dividerColor = Color.WHITE;
                style.buttonTextColor = Color.GRAY;
                return style;

            case PURPLE:

                style.textColor = Color.WHITE;
                style.background = getBackground(PURPLE);
                style.dividerColor = Color.WHITE;
                return style;

            case RED:

                style.textColor = Color.WHITE;
                style.background = getBackground(RED);
                style.dividerColor = Color.WHITE;
                return style;

            case ORANGE:

                style.textColor = Color.WHITE;
                style.background = getBackground(ORANGE);
                style.dividerColor = Color.WHITE;
                return style;

            case BLUE:

                style.textColor = Color.WHITE;
                style.background = getBackground(BLUE);
                style.dividerColor = Color.WHITE;
                return style;

            case GREEN:

                style.textColor = Color.WHITE;
                style.background = getBackground(GREEN);
                style.dividerColor = Color.WHITE;
                return style;

            default:

                style.textColor = Color.WHITE;
                style.background = getBackground(GRAY);
                style.dividerColor = Color.WHITE;
                return style;

        }

    }

    public static int getBackground(int style) {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            switch (style) {

                case BLACK:

                    return (R.drawable.background_kitkat_black);

                case WHITE:

                    return (R.drawable.background_kitkat_white);

                case GRAY:

                    return (R.drawable.background_kitkat_gray);

                case PURPLE:

                    return (R.drawable.background_kitkat_purple);

                case RED:

                    return (R.drawable.background_kitkat_red);

                case ORANGE:

                    return (R.drawable.background_kitkat_orange);

                case BLUE:

                    return (R.drawable.background_kitkat_blue);

                case GREEN:

                    return (R.drawable.background_kitkat_green);

                default:

                    return (R.drawable.background_kitkat_gray);

            }

        } else {

            switch (style) {

                case BLACK:

                    return (R.drawable.background_standard_black);

                case WHITE:

                    return (R.drawable.background_standard_white);

                case GRAY:

                    return (R.drawable.background_standard_gray);

                case PURPLE:

                    return (R.drawable.background_standard_purple);

                case RED:

                    return (R.drawable.background_standard_red);

                case ORANGE:

                    return (R.drawable.background_standard_orange);

                case BLUE:

                    return (R.drawable.background_standard_blue);

                case GREEN:

                    return (R.drawable.background_standard_green);

                default:

                    return (R.drawable.background_standard_gray);

            }

        }

    }

}
public class SuperToast {

    private static final String TAG = "SuperToast";

    private static final String ERROR_CONTEXTNULL = " - You cannot use a null context.";
    private static final String ERROR_DURATIONTOOLONG = " - You should NEVER specify a duration greater than " +
            "four and a half seconds for a SuperToast.";

    /**
     * Custom OnClickListener to be used with SuperActivityToasts/SuperCardToasts. Note that
     * SuperActivityToasts/SuperCardToasts must use this with an
     * {@link com.github.johnpersano.supertoasts.util.OnClickWrapper}
     */
    public interface OnClickListener {

        public void onClick(View view, Parcelable token);

    }

    /**
     * Custom OnDismissListener to be used with any type of SuperToasts. Note that
     * SuperActivityToasts/SuperCardToasts must use this with an
     * {@link com.github.johnpersano.supertoasts.util.OnDismissWrapper}
     */
    public interface OnDismissListener {

        public void onDismiss(View view);

    }

    /**
     * Backgrounds for all types of SuperToasts.
     */
    public static class Background {

        public static final int BLACK = Style.getBackground(Style.BLACK);
        public static final int BLUE = Style.getBackground(Style.BLUE);
        public static final int GRAY = Style.getBackground(Style.GRAY);
        public static final int GREEN = Style.getBackground(Style.GREEN);
        public static final int ORANGE = Style.getBackground(Style.ORANGE);
        public static final int PURPLE = Style.getBackground(Style.PURPLE);
        public static final int RED = Style.getBackground(Style.RED);
        public static final int WHITE = Style.getBackground(Style.WHITE);

    }

    /**
     * Animations for all types of SuperToasts.
     */
    public enum Animations {

        FADE,
        FLYIN,
        SCALE,
        POPUP

    }

    /**
     * Icons for all types of SuperToasts.
     */
    public static class Icon {

        /**
         * Icons for all types of SuperToasts with a dark background.
         */
        public static class Dark {

            public static final int EDIT = (R.drawable.icon_dark_edit);
            public static final int EXIT = (R.drawable.icon_dark_exit);
            public static final int INFO = (R.drawable.icon_dark_info);
            public static final int REDO = (R.drawable.icon_dark_redo);
            public static final int REFRESH = (R.drawable.icon_dark_refresh);
            public static final int SAVE = (R.drawable.icon_dark_save);
            public static final int SHARE = (R.drawable.icon_dark_share);
            public static final int UNDO = (R.drawable.icon_dark_undo);

        }

        /**
         * Icons for all types of SuperToasts with a light background.
         */
        public static class Light {

            public static final int EDIT = (R.drawable.icon_light_edit);
            public static final int EXIT = (R.drawable.icon_light_exit);
            public static final int INFO = (R.drawable.icon_light_info);
            public static final int REDO = (R.drawable.icon_light_redo);
            public static final int REFRESH = (R.drawable.icon_light_refresh);
            public static final int SAVE = (R.drawable.icon_light_save);
            public static final int SHARE = (R.drawable.icon_light_share);
            public static final int UNDO = (R.drawable.icon_light_undo);

        }

    }

    /**
     * Durations for all types of SuperToasts.
     */
    public static class Duration {

        public static final int VERY_SHORT = (1500);
        public static final int SHORT = (2000);
        public static final int MEDIUM = (2750);
        public static final int LONG = (3500);
        public static final int EXTRA_LONG = (4500);

    }

    /**
     * Text sizes for all types of SuperToasts.
     */
    public static class TextSize {

        public static final int EXTRA_SMALL = (12);
        public static final int SMALL = (14);
        public static final int MEDIUM = (16);
        public static final int LARGE = (18);

    }

    /**
     * Types for SuperActivityToasts and SuperCardToasts.
     */
    public enum Type {

        /**
         * Standard type used for displaying messages.
         */
        STANDARD,

        /**
         * Progress type used for showing progress.
         */
        PROGRESS,

        /**
         * Progress type used for showing progress.
         */
        PROGRESS_HORIZONTAL,

        /**
         * Button type used for receiving click actions.
         */
        BUTTON

    }

    /**
     * Positions for icons used in all types of SuperToasts.
     */
    public enum IconPosition {

        /**
         * Set the icon to the left of the text.
         */
        LEFT,

        /**
         * Set the icon to the right of the text.
         */
        RIGHT,

        /**
         * Set the icon on top of the text.
         */
        TOP,

        /**
         * Set the icon on the bottom of the text.
         */
        BOTTOM

    }

    private Animations mAnimations = Animations.FADE;
    private Context mContext;
    private int mGravity = Gravity.BOTTOM | Gravity.CENTER;
    private int mDuration = Duration.SHORT;
    private int mTypefaceStyle;
    private int mBackground;
    private int mXOffset = 0;
    private int mYOffset = 0;
    private LinearLayout mRootLayout;
    private OnDismissListener mOnDismissListener;
    private TextView mMessageTextView;
    private View mToastView;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowManagerParams;

    /**
     * Instantiates a new {@value #TAG}.
     *
     * @param context {@link android.content.Context}
     */
    public SuperToast(Context context) {

        if (context == null) {

            throw new IllegalArgumentException(TAG + ERROR_CONTEXTNULL);

        }

        this.mContext = context;

        mYOffset = context.getResources().getDimensionPixelSize(
                R.dimen.toast_hover);

        final LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mToastView = layoutInflater.inflate(R.layout.supertoast, null);

        mWindowManager = (WindowManager) mToastView.getContext()
                .getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        mRootLayout = (LinearLayout)
                mToastView.findViewById(R.id.root_layout);

        mMessageTextView = (TextView)
                mToastView.findViewById(R.id.message_textview);

    }

    /**
     * Instantiates a new {@value #TAG} with a specified style.
     *
     * @param context {@link android.content.Context}
     * @param style   {@link com.github.johnpersano.supertoasts.util.Style}
     */
    public SuperToast(Context context, Style style) {

        if (context == null) {

            throw new IllegalArgumentException(TAG + ERROR_CONTEXTNULL);

        }

        this.mContext = context;

        mYOffset = context.getResources().getDimensionPixelSize(
                R.dimen.toast_hover);

        final LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mToastView = layoutInflater.inflate(R.layout.supertoast, null);

        mWindowManager = (WindowManager) mToastView.getContext()
                .getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        mRootLayout = (LinearLayout)
                mToastView.findViewById(R.id.root_layout);

        mMessageTextView = (TextView)
                mToastView.findViewById(R.id.message_textview);

        this.setStyle(style);

    }

    /**
     * Shows the {@value #TAG}. If another {@value #TAG} is showing than
     * this one will be added to a queue and shown when the previous {@value #TAG}
     * is dismissed.
     */
    public void show() {

        mWindowManagerParams = new WindowManager.LayoutParams();

        mWindowManagerParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        mWindowManagerParams.format = PixelFormat.TRANSLUCENT;
        mWindowManagerParams.windowAnimations = getAnimation();
        mWindowManagerParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mWindowManagerParams.gravity = mGravity;
        mWindowManagerParams.x = mXOffset;
        mWindowManagerParams.y = mYOffset;

        ManagerSuperToast.getInstance().add(this);

    }

    /**
     * Sets the message text of the {@value #TAG}.
     *
     * @param text {@link CharSequence}
     */
    public void setText(CharSequence text) {

        mMessageTextView.setText(text);

    }

    /**
     * Returns the message text of the {@value #TAG}.
     *
     * @return {@link CharSequence}
     */
    public CharSequence getText() {

        return mMessageTextView.getText();

    }

    /**
     * Sets the message typeface style of the {@value #TAG}.
     *
     * @param typeface {@link android.graphics.Typeface} int
     */
    public void setTypefaceStyle(int typeface) {

        mTypefaceStyle = typeface;

        mMessageTextView.setTypeface(mMessageTextView.getTypeface(), typeface);

    }

    /**
     * Returns the message typeface style of the {@value #TAG}.
     *
     * @return {@link android.graphics.Typeface} int
     */
    public int getTypefaceStyle() {

        return mTypefaceStyle;

    }

    /**
     * Sets the message text color of the {@value #TAG}.
     *
     * @param textColor {@link android.graphics.Color}
     */
    public void setTextColor(int textColor) {

        mMessageTextView.setTextColor(textColor);

    }

    /**
     * Returns the message text color of the {@value #TAG}.
     *
     * @return int
     */
    public int getTextColor() {

        return mMessageTextView.getCurrentTextColor();

    }

    /**
     * Sets the text size of the {@value #TAG} message.
     *
     * @param textSize int
     */
    public void setTextSize(int textSize) {

        mMessageTextView.setTextSize(textSize);

    }

    /**
     * Returns the text size of the {@value #TAG} message in pixels.
     *
     * @return float
     */
    public float getTextSize() {

        return mMessageTextView.getTextSize();

    }

    /**
     * Sets the duration that the {@value #TAG} will show.
     *
     * @param duration {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
     */
    public void setDuration(int duration) {

        if(duration > Duration.EXTRA_LONG) {

            Log.e(TAG, TAG + ERROR_DURATIONTOOLONG);

            this.mDuration = Duration.EXTRA_LONG;

        } else {

            this.mDuration = duration;

        }

    }

    /**
     * Returns the duration of the {@value #TAG}.
     *
     * @return int
     */
    public int getDuration() {

        return this.mDuration;

    }

    /**
     * Sets an icon resource to the {@value #TAG} with a specified position.
     *
     * @param iconResource {@link com.github.johnpersano.supertoasts.SuperToast.Icon}
     * @param iconPosition {@link com.github.johnpersano.supertoasts.SuperToast.IconPosition}
     */
    public void setIcon(int iconResource, IconPosition iconPosition) {

        if (iconPosition == IconPosition.BOTTOM) {

            mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    null, mContext.getResources().getDrawable(iconResource));

        } else if (iconPosition == IconPosition.LEFT) {

            mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
                    .getDrawable(iconResource), null, null, null);

        } else if (iconPosition == IconPosition.RIGHT) {

            mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    mContext.getResources().getDrawable(iconResource), null);

        } else if (iconPosition == IconPosition.TOP) {

            mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null,
                    mContext.getResources().getDrawable(iconResource), null, null);

        }

    }

    /**
     * Sets the background resource of the {@value #TAG}.
     *
     * @param background {@link com.github.johnpersano.supertoasts.SuperToast.Background}
     */
    public void setBackground(int background) {

        this.mBackground = background;

        mRootLayout.setBackgroundResource(background);

    }

    /**
     * Returns the background resource of the {@value #TAG}.
     *
     * @return int
     */
    public int getBackground() {

        return this.mBackground;

    }

    /**
     * Sets the gravity of the {@value #TAG} along with x and y offsets.
     *
     * @param gravity {@link android.view.Gravity} int
     * @param xOffset int
     * @param yOffset int
     */
    public void setGravity(int gravity, int xOffset, int yOffset) {

        this.mGravity = gravity;
        this.mXOffset = xOffset;
        this.mYOffset = yOffset;

    }

    /**
     * Sets the show/hide animations of the {@value #TAG}.
     *
     * @param animations {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
     */
    public void setAnimations(Animations animations) {

        this.mAnimations = animations;

    }

    /**
     * Returns the show/hide animations of the {@value #TAG}.
     *
     * @return {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
     */
    public Animations getAnimations() {

        return this.mAnimations;

    }

    /**
     * Sets an OnDismissListener defined in this library
     * to the {@value #TAG}. Does not require wrapper.
     *
     * @param onDismissListener {@link com.github.johnpersano.supertoasts.SuperToast.OnDismissListener}
     */
    public void setOnDismissListener(OnDismissListener onDismissListener) {

        this.mOnDismissListener = onDismissListener;

    }

    /**
     * Returns the OnDismissListener set to the {@value #TAG}.
     *
     * @return {@link com.github.johnpersano.supertoasts.SuperToast.OnDismissListener}
     */
    public OnDismissListener getOnDismissListener() {

        return mOnDismissListener;

    }

    /**
     * Dismisses the {@value #TAG}.
     */
    public void dismiss() {

        ManagerSuperToast.getInstance().removeSuperToast(this);

    }

    /**
     * Returns the {@value #TAG} message textview.
     *
     * @return {@link android.widget.TextView}
     */
    public TextView getTextView() {

        return mMessageTextView;

    }

    /**
     * Returns the {@value #TAG} view.
     *
     * @return {@link android.view.View}
     */
    public View getView() {

        return mToastView;

    }

    /**
     * Returns true if the {@value #TAG} is showing.
     *
     * @return boolean
     */
    public boolean isShowing() {

        return mToastView != null && mToastView.isShown();

    }

    /**
     * Returns the window manager that the {@value #TAG} is attached to.
     *
     * @return {@link android.view.WindowManager}
     */
    public WindowManager getWindowManager() {

        return mWindowManager;

    }

    /**
     * Returns the window manager layout params of the {@value #TAG}.
     *
     * @return {@link android.view.WindowManager.LayoutParams}
     */
    public WindowManager.LayoutParams getWindowManagerParams() {

        return mWindowManagerParams;

    }

    /**
     * Private method used to return a specific animation for a animations enum
     */
    private int getAnimation() {

        if (mAnimations == Animations.FLYIN) {

            return android.R.style.Animation_Translucent;

        } else if (mAnimations == Animations.SCALE) {

            return android.R.style.Animation_Dialog;

        } else if (mAnimations == Animations.POPUP) {

            return android.R.style.Animation_InputMethod;

        } else {

            return android.R.style.Animation_Toast;

        }

    }

    /**
     * Private method used to set a default style to the {@value #TAG}
     */
    private void setStyle(Style style) {

        this.setAnimations(style.animations);
        this.setTypefaceStyle(style.typefaceStyle);
        this.setTextColor(style.textColor);
        this.setBackground(style.background);

    }

    /**
     * Returns a standard {@value #TAG}.
     *
     * @param context          {@link android.content.Context}
     * @param textCharSequence {@link CharSequence}
     * @param durationInteger  {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
     *
     * @return {@link SuperToast}
     */
    public static SuperToast create(Context context, CharSequence textCharSequence,
                                              int durationInteger) {

        SuperToast superToast = new SuperToast(context);
        superToast.setText(textCharSequence);
        superToast.setDuration(durationInteger);

        return superToast;

    }

    /**
     * Returns a standard {@value #TAG} with specified animations.
     *
     * @param context          {@link android.content.Context}
     * @param textCharSequence {@link CharSequence}
     * @param durationInteger  {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
     * @param animations       {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
     *
     * @return {@link SuperToast}
     */
    public static SuperToast create(Context context, CharSequence textCharSequence,
                                              int durationInteger, Animations animations) {

        final SuperToast superToast = new SuperToast(context);
        superToast.setText(textCharSequence);
        superToast.setDuration(durationInteger);
        superToast.setAnimations(animations);

        return superToast;

    }

    /**
     * Returns a {@value #TAG} with a specified style.
     *
     * @param context          {@link android.content.Context}
     * @param textCharSequence {@link CharSequence}
     * @param durationInteger  {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
     * @param style            {@link com.github.johnpersano.supertoasts.util.Style}
     *
     * @return SuperCardToast
     */
    public static SuperToast create(Context context, CharSequence textCharSequence, int durationInteger, Style style) {

        final SuperToast superToast = new SuperToast(context);
        superToast.setText(textCharSequence);
        superToast.setDuration(durationInteger);
        superToast.setStyle(style);

        return superToast;

    }

    /**
     * Dismisses and removes all showing/pending {@value #TAG}.
     */
    public static void cancelAllSuperToasts() {

        ManagerSuperToast.getInstance().cancelAllSuperToasts();

    }

}

Code见 https://github.com/huanyi0723/TestToast

推荐阅读