平时通常只用默认的Toast,使用Toast.makeTest()方法调用,默认的风格是白字半透明灰框,常常与app的主题颜色不符,因此须要自定义Toast.效果图:html
显示定义须要的布局文件:layout/custom_toast.xmljava
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_container" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>
注意root view 的I必须设置id(接下来代码用到,此时为custom_toast_container)android
接下来是kotlin代码(根据android官网,手动将Java代码改成Kotliin代码)安全
class ToastActivity:AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) var layoutInflater:LayoutInflater = layoutInflater //这里须要一个安全类型转换as?, 否则编译没法经过,由于ViewGroup是not null类型, //而findViewById(R.id.custom_toast_container)可能为null,因此不能直接用as var layout: View = layoutInflater.inflate(R.layout .custom_toast, findViewById(R.id.custom_toast_container) as? ViewGroup) val text:TextView = layout.findViewById(R.id.text) as TextView text.setText("This is a custom toast") //这个若是是java语法,则须要调用getApplicationContext, val toast = Toast(applicationContext) toast.setGravity(Gravity.CENTER_VERTICAL, 0, 300)//设置位置 toast.duration = Toast.LENGTH_LONG toast.view = layout//java:toast.setView(layout); toast.show() } }
代码都是官方文档拿过来的,里面是Java代码:
https://developer.android.com...app