【发布时间】:2021-10-10 19:28:47
【问题描述】:
我是 Android 开发新手,我正在尝试创建一个简单的通知,告诉用户何时完成某些用户操作。
我希望通知显示在屏幕顶部并在不久后自行消失。基本上我想干杯。
但是,在 Android 30+ 中,Toast.makeText() 方法不允许设置 toast 位置的选项,当我创建自定义 toast 时,我收到一条警告说设置视图已被贬值。
answer to this post recommends 使用了一个snackbar,但我也找不到一种优雅的方式来将snackbar 设置为出现在屏幕顶部。
实现我的目标的最佳方式是什么?
这是将 toast 放到屏幕顶部的折旧代码尝试
val inflater = layoutInflater
val layout: View = inflater.inflate(
R.layout.custom_toast, null
)
val text: TextView = layout.findViewById(R.id.text)
text.text = "Hello! This is a custom toast!"
val toast = Toast(applicationContext)
toast.setGravity(Gravity.TOP, 0, 0)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()
【问题讨论】:
-
您不能为此使用 toast,您必须自己实现或使用外部库
标签: android kotlin toast android-snackbar