自定义布局
public class ToastUtil {
private static Toast toast;
private static View v;
private static TextView tv;
public static void show(CharSequence text) {
if (toast==null){
toast = new Toast(MyApp.getInstance());
}
if (v==null){
v = LayoutInflater.from(MyApp.getInstance()).inflate(R.layout.toast, null);
tv = v.findViewById(R.id.tv_msg);
toast.setView(v);
}
tv.setText(text);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
}
非自定义布局
public static void showToast(String msg){
if (toast == null){
toast = Toast.makeText(MyApp.getInstance(),msg,Toast.LENGTH_SHORT);
}else {
toast.setText(msg);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
}