在使用自定义 Toast 的时候,需要先对 inflater 进行初始化。代码示例如下:
 LayoutInflater inflater = getLayoutInflater();
 View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root));
 Toast toast = new Toast(getApplicationContext());
 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 toast.setDuration(Toast.LENGTH_SHORT);
 toast.setView(layout);
 toast.show();
其中 custom_toast 是自定义 Toast 的布局文件,在使用自定义 Toast 的时候需要先将它加载进来。通过 inflater.inflate() 方法将布局文件转换成 View 并返回给 layout 变量,然后再将 layout 设置为 Toast 的 View,最后调用 toast.show() 显示 Toast 即可。