-
Notifications
You must be signed in to change notification settings - Fork 80
classic toast api for Kotlin
vincent(朱志强) edited this page Jun 8, 2024
·
21 revisions
- 快速上手
- 配置风格
//string msg
SmartToast.classic().show("I'm a toast")
//resource msg
SmartToast.classic().show(R.string.toast_msg)
//string msg
SmartToast.classic().showInCenter("I'm a toast")
//resource msg
SmartToast.classic().showInCenter(R.string.toast_msg)
//string msg
SmartToast.classic().showAtTop("I'm a toast")
//resource msg
SmartToast.classic().showAtTop(R.string.toast_msg)
//string msg
SmartToast.classic().showAtLocation("I'm a toast",gravity, xOffsetDp, yOffsetDp)
//resource msg
SmartToast.classic().showAtLocation(R.string.toast_msg,gravity, xOffsetDp, yOffsetDp)
需要配置风格时,调用config()
方法,配置结束后,调用commit
方法生效
SmartToast.classic()
.config()
//drawable bg
.backgroundDrawable(drawable)
.commit()
.show("I'm a toast")
SmartToast.classic()
.config()
//resource bg
.backgroundResource(R.drawable.toast_bg)
.commit()
.show("I'm a toast")
回到目录导航
配置颜色,并不会替换默认的背景drawable,只会替换其填充色
SmartToast.classic()
.config()
//color int
.backgroundColor(Color.BLUE)
.commit()
.show("I'm a toast")
SmartToast.classic()
.config()
//color resource
.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()
//drawable icon
.iconDrawable(drawable)
.commit()
.show("I'm a toast")
SmartToast.classic()
.config()
//resource icon
.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
,单位为毫秒
SmartToast.classic()
.config()
//显示时长设置为5秒
.duration(Duration.of(5000))
.commit()
.show("custom duration")
1