提示窗Toast
大家平时都经常用Toast,可是你是否发现,系统默认的Toast样式太过单调乏味呢?其实Toast的界面也允许开发者自行定制,只要定义好提示窗的布局文件,即可调用Toast类的setView方法设置自定义窗口画面。包括背景、对齐方式、窗口内部控件等等界面元素,均可由你自己定制。下面是自定义提示窗的两个截图,分别展示了不同背景与不同对齐方式下的界面效果:
下面是自定义提示窗的代码例子:
Toast toast = new Toast(this);
View vv = LayoutInflater.from(this).inflate(R.layout.toast_hint, null);
TextView tv_toast = (TextView) vv.findViewById(R.id.tv_toast);
tv_toast.setText(text);
LinearLayout ll_toast = (LinearLayout) vv.findViewById(R.id.ll_toast);
ll_toast.setBackgroundColor(mBackground);
toast.setView(vv);
toast.setGravity(mGravity, 0, 0);
toast.setDuration(duration);
toast.show();