Android Toast 使用

基本的使用

val text = "Hello toast!"
val duration = Toast.LENGTH_SHORT

val toast = Toast.makeText(applicationContext, text, duration)
toast.show()

或者

Toast.makeText(context, text, duration).show()

設定顯示位置

toast.setGravity(Gravity.TOP or Gravity.LEFT, 0, 0)

自定義自己Toast

  1. xml (saved as layout/custom_toast.xml)

    
    

val inflater = layoutInflater
val container: ViewGroup = findViewById(R.id.custom_toast_container)
val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container)
val text: TextView = layout.findViewById(R.id.text)
text.text = "This is a custom toast"
with (Toast(applicationContext)) {
    setGravity(Gravity.CENTER_VERTICAL, 0, 0)
    duration = Toast.LENGTH_LONG
    view = layout
    show()
}

基本上是這樣 如有其他方式再更新

你可能感兴趣的:(Android Toast 使用)