如何使用 Kotlin 在 Android 中添加自定义样式的 Toast
Toast是在 Android 屏幕上显示一小段时间的简短警报消息。 Android Toast是一个简短的弹出通知,用于在我们在应用程序中执行任何操作时显示信息。在本文中,让我们学习如何使用 Kotlin 在 Android 中创建自定义 toast。
Note: To create custom styled toast in Android using Java please refer to How to add a custom styled Toast in Android.
属性表
Attributes | Description |
---|---|
LayoutInflater | Instantiates a layout XML file into its corresponding View objects |
inflate | Inflate a new view hierarchy from the specified XML resource. |
setGravity | Used to change the position of Toast |
方法
第 1 步:创建 Toast 布局
转到res -> 布局(右键单击)-> 新建 -> 布局资源文件 -> 创建(custom_toast_layout.xml)文件。添加一个 CardView 以包含自定义 toast 消息,并添加一个 TextView 以显示自定义 toast 消息中的文本。 FrameLayout 用于指定多个视图相互重叠放置的位置,以表示单个视图屏幕。
XML
Kotlin
import android.app.Activity
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
fun Toast.showCustomToast(message: String, activity: Activity)
{
val layout = activity.layoutInflater.inflate (
R.layout.custom_toast_layout,
activity.findViewById(R.id.toast_container)
)
// set the text of the TextView of the message
val textView = layout.findViewById(R.id.toast_text)
textView.text = message
// use the application extension function
this.apply {
setGravity(Gravity.BOTTOM, 0, 40)
duration = Toast.LENGTH_LONG
view = layout
show()
}
}
XML
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// apply an onClickListener() method
btn_show_toast.setOnClickListener{
Toast(this).showCustomToast ("Hello! This is a custom Toast!", this)
}
}
}
第 2 步:创建一个新的Kotlin 文件
现在创建一个新的 Kotlin 文件并将其命名为WrapToast.kt以使代码可重用。转到项目包(右键单击)-> 新建-> Kotlin 文件/类-> 创建 (WrapToast.kt) 文件。现在我们将使用showCustomToast()扩展Toast::class ,它将 String 和 Context 作为参数。
Note:
- Inflate the previously created layout (custom_toast_layout.xml) using the layoutInflater.
- After, inflated the layout, find its view. In this case, set the text of the TextView of the message.
- The last step is to create a new instance about the Toast:: class. Then, using its application extension function sets the gravity, the duration, and the layout. Inside apply, call the show() method as well.
科特林
import android.app.Activity
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
fun Toast.showCustomToast(message: String, activity: Activity)
{
val layout = activity.layoutInflater.inflate (
R.layout.custom_toast_layout,
activity.findViewById(R.id.toast_container)
)
// set the text of the TextView of the message
val textView = layout.findViewById(R.id.toast_text)
textView.text = message
// use the application extension function
this.apply {
setGravity(Gravity.BOTTOM, 0, 40)
duration = Toast.LENGTH_LONG
view = layout
show()
}
}
第 3 步:创建一个按钮以在 Activity 中显示 Toast
在 ConstraintLayout 中添加一个 Button。因此,当用户单击按钮时,屏幕上会弹出自定义 Toast。
XML
第 4 步:创建吐司
之后,创建按钮以显示 toast 应用onClickListener()并传递 Toast 消息和活动的上下文。
科特林
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// apply an onClickListener() method
btn_show_toast.setOnClickListener{
Toast(this).showCustomToast ("Hello! This is a custom Toast!", this)
}
}
}
输出:
Note:
Custom toast views are no longer recommended. When in the foreground, apps can use the makeText() function to produce a normal text toast, or they can create a Snackbar. Custom toast views will not be displayed when the owning application, targeting API level Build.VERSION_CODES#R or above is in the background. For now, Toasts built using makeText() or its variations will likewise return null here in apps targeting API level Build.VERSION CODES.R or above, unless they called setView with a non-null view.