1,添加依賴
2,在application中初始化
SmartShow.init(this);
SmartToast.setting()
.dismissOnLeave(true); // 離開目前Activity,Toast自動消失
3,工具類
public class ToastUtils {
// 預設底部顯示
public static void ToastShow(CharSequence msg){
SmartToast.show(msg);
}
public static void ToastShow(int msg){
SmartToast.show(msg);
}
public static void ToastShowLong(CharSequence msg){
SmartToast.show(msg);
}
public static void ToastShowLong(int msg){
SmartToast.show(msg);
}
// 中間顯示
public static void showInCenter(CharSequence msg){
SmartToast.showInCenter(msg);
}
public static void showInCenter(int msg){
SmartToast.showInCenter(msg);
}
public static void showInCenterLong(CharSequence msg){
SmartToast.showLongInCenter(msg);
}
public static void showInCenterLong(int msg){
SmartToast.showLongInCenter(msg);
}
// 頂部顯示
public static void showAtTop(CharSequence msg){
SmartToast.showAtTop(msg);
}
public static void showAtTop(int msg){
SmartToast.showAtTop(msg);
}
public static void showAtTopLong(CharSequence msg){
SmartToast.showLongAtTop(msg);
}
public static void showAtTopLong(int msg){
SmartToast.showLongAtTop(msg);
}
//如,在左上角,x,y偏移量為10dp的位置顯示
// SmartToast.showAtLocation(msg,Gravity.LEFT | Gravity.TOP,10,10);
// 自定義顯示位置
public static void showAtLocation(CharSequence msg, int gravity, float xOffsetDp, float yOffsetDp){
SmartToast.showAtLocation(msg,gravity,xOffsetDp,yOffsetDp);
}
public static void showAtLocation(int msg, int gravity, float xOffsetDp, float yOffsetDp){
SmartToast.showAtLocation(msg,gravity,xOffsetDp,yOffsetDp);
}
public static void showLongAtLocation(CharSequence msg, int gravity, float xOffsetDp, float yOffsetDp){
SmartToast.showLongAtLocation(msg,gravity,xOffsetDp,yOffsetDp);
}
public static void showLongAtLocation(int msg, int gravity, float xOffsetDp, float yOffsetDp){
SmartToast.showLongAtLocation(msg,gravity,xOffsetDp,yOffsetDp);
}
}
頂部彈出的SnackBar工具類:
添加依賴
在application中初始化初始化:
SmartTopbar.setting()
.backgroundColorRes(R.color.colorPrimary) // 設定背景顔色
.msgTextColorRes(R.color.colorAccent) // 設定消息文本顔色
.msgTextSizeSp(14) // 設定消息文本大小
.actionColorRes(R.color.colorAccent) // 設定動作文本顔色
.actionSizeSp(14) // 設定動作文本大小
.dismissOnLeave(true); // 離開目前Activity,Toast自動消失
public class TopSnackBarUtils {
// 單純顯示一句提示
public static void show(Activity activity,CharSequence msg){
SmartTopbar.get(activity).show(msg); // 和Toast一樣,會自動消失
}
public static void showAction(Activity activity, CharSequence msg, CharSequence msgaction, View.OnClickListener clist){
SmartTopbar.get(activity).showIndefinite(msg, msgaction,clist); // 不會消失,點選action才會消失
}
}