1、创建一个tosat显示的布局文件:toast_custom.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<com.hjq.shape.layout.ShapeLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:orientation="vertical"
android:paddingVertical="@dimen/dp_20"
android:paddingHorizontal="@dimen/dp_30"
app:shape_radius="@dimen/dp_10"
app:shape_solidColor="#B3000000">
<ImageView
android:id="@+id/toast_icon"
android:layout_width="@dimen/dp_56"
android:layout_height="@dimen/dp_56"
android:src="@mipmap/toast_icon" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="保存成功"
android:textColor="@color/white"
android:textSize="16sp" />
</com.hjq.shape.layout.ShapeLinearLayout>
2.通过代码显示出来:
Kotlin
private fun showToast(text : String = "保存成功") {
Toast(applicationContext).apply {
duration = Toast.LENGTH_SHORT
view = layoutInflater.inflate(R.layout.toast_custom, null)
val tv = view?.findViewById<TextView>(R.id.toast_text)
tv?.text = text
show()
}
}
直接调用方法就行!