自定义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

Android 自定义Toast的更多相关文章

  1. 朝花夕拾-android 自定义toast

    在一个只有你而且还未知的世界中,不去探索未知,死守一处,你到底在守什么呢? 作为一个目前的android程序员,可能过去写着delphi的代码,可能未来回去搭建服务器.不管怎样,你现在是一名安卓程序员 ...

  2. android 自定义Toast显示风格

    1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...

  3. 023 Android 自定义Toast控件

    1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...

  4. Android 自定义Toast,不使用系统Toast

    效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...

  5. Android自定义Toast宽度无法设置问题解决

    在项目中想要实现一个头部的toast提示效果,类似下图  再实现的过程中发现,如果直接通过修改Toast的View布局的父控件宽度是无法实现效果的,后来是通过直接用代码指定父控件内部的textview ...

  6. Android自定义Toast

    1.http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html 2.

  7. Android带图片的Toast(自定义Toast)

    使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...

  8. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  9. Android开发必知--自定义Toast提示

    开发过Android的童鞋都会遇到一个问题,就是在打印Toast提示时,如果短时间内触发多个提示,就会造成Toast不停的重复出现,直到被触发的Toast全部显示完为止.这虽然不是什么大毛病,但在用户 ...

随机推荐

  1. [C++]C++标准里 string和wstring

    typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; 前者string是常用类型, ...

  2. ACM题目————最短路径问题

    Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input 输入n,m,点 ...

  3. redis初试牛刀

    先来无事就学学redis.可是并没有想的那么美好.首先要解释一下,redis主流是安装在lunx系统中的,甚至官网直接没有给出windows版本.要下载windows只能去所谓的githup.好吧我在 ...

  4. API判断网站IP地址,国家区域

    直接访问http://api.wipmania.com/jsonp 还有经纬度

  5. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  6. python Flask restful框架

    框架地址:https://github.com/flask-restful/flask-restful 文档:http://flask-restful.readthedocs.io/en/0.3.5/ ...

  7. java-cmd-命令行编译和运行java文件

    一.使用的工具 1.javac 2.java 二.命令 项目目录只这样的 D:/project/src/com/example/Child.java D:/project/src/com/exampl ...

  8. iOS开发debug跟release版本屏蔽NSLog方法

    1.在***-Prefix.pch里面添加 #ifndef __OPTIMIZE__ # define NSLog(...) NSLog(__VA_ARGS__) #else # define NSL ...

  9. 如何重置CentOS/RHEL 7中遗忘的根用户帐户密码

    你有没有遇到过这种情况:想不起来Linux系统上的用户帐户密码?要是你忘了根用户密码,情况就更为糟糕.你无法执行任何面向整个系统的变更.要是你忘了用户密码,很容易使用根帐户来重置密码. 可要是你忘了根 ...

  10. BZOJ 3546 Life of the Party (二分图匹配-最大流)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3546 题意:给定一个二分图.(AB两个集合的点为n,m),边有K个.问去掉哪些点后 ...