-
Notifications
You must be signed in to change notification settings - Fork 80
classic toast api for Java
vincent(朱志强) edited this page May 7, 2024
·
5 revisions
- 快速上手
- 配置风格
//short toast
SmartToast.classic().show("I'm a short bottom toast");
//long toast
SmartToast.classic().showLong("I'm a long bottom toast");
或者,
//short toast
SmartToast.classic().show(R.string.toast_msg);
//long toast
SmartToast.classic().showLong(R.string.toast_msg);
//short toast
SmartToast.classic().showInCenter("I'm a short central toast");
//long toast
SmartToast.classic().showLongInCenter("I'm a long central toast");
或者,
SmartToast.classic().showInCenter(R.string.toast_msg);
SmartToast.classic().showLongInCenter(R.string.toast_msg);
SmartToast.classic().showAtTop("I'm a short top toast");
SmartToast.classic().showLongAtTop("I'm a long top toast");
或者,
SmartToast.classic().showAtTop(R.string.toast_msg);
SmartToast.classic().showLongAtTop(R.string.toast_msg);
//short toast
SmartToast.classic().showAtLocation("I'm a short toast",gravity, xOffsetDp, yOffsetDp);
//long toast
SmartToast.classic().showLongAtLocation("I'm a long toast",gravity, xOffsetDp, yOffsetDp);
或者,
//short toast
SmartToast.classic().showAtLocation(R.string.toast_msg,gravity, xOffsetDp, yOffsetDp);
//long toast
SmartToast.classic().showLongAtLocation(R.string.toast_msg,gravity, xOffsetDp, yOffsetDp);
需要配置风格时,调用config()
方法,配置结束后,调用commit
方法生效
SmartToast.classic()
.config()
.backgroundDrawable(drawable)
.commit()
.show("I'm a toast");
或者,
SmartToast.classic()
.config()
.backgroundResource(R.drawable.toast_bg)
.commit()
.show("I'm a toast");
回到目录导航
配置颜色,并不会替换默认的背景drawable,只会替换其填充色
SmartToast.classic()
.config()
.backgroundColor(Color.BLUE)
.commit()
.show("I'm a toast");
或者,
SmartToast.classic()
.config()
.backgroundColorResource(R.color.colorPrimary)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
//颜色,大小,是否加粗
.messageStyle(Color.WHITE, 14f, false)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
.iconDrawable(drawable)
.commit()
.show("I'm a toast");
或者,
SmartToast.classic()
.config()
.iconResource(R.drawable.ic_toast_success)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
//单位dp
.iconSize(14f)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
//IconPosition.LEFT,IconPosition.RIGHT
.iconPosition(IconPosition.LEFT)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
.marginBetweenIconAndMsg(10f)
.commit()
.show("I'm a toast");
SmartToast.classic()
.config()
//short toast,也是默认的配置
.duration(Duration.SHORT)
.commit()
.show("custom duration");
duration方法的参数必须是Duration对象,有三个内置值:
-
Duration.SHORT
2s的toast,也是不配置duration时的默认值 -
Duration.LONG
,3.5s的toast -
Duration.INDEFINITE
一直展示,直到被下一个toast覆盖,或主动调用SmartToast.dismiss()方法关闭
此外,可通过Duration.of
方法,获取任意时长的Duration,单位为毫秒
Duration.of(5000);
1