⒈用途
- Toast是一個消息提示組件
- 可以設置顯示的位置(自己有默認位置)
- 自定義顯示內容(例如:添加一個圖片)
- 簡單封裝
⒉使用
默認
Toast.makeText(getApplicationContext(),"",Toast.LENGTH_LONG).show();
居中彈出
Toast toastCenter = Toast.makeText(getApplicationContext(),"",Toast.LENGTH_LONG); toastCenter.setGravity(Gravity.CENTER,0,0); toastCenter.show();
帶圖片的消息彈出
Toast toastCustom = new Toast(getApplicationContext()); LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); View view = inflater.inflate(R.layout.layout_toast,null); ImageView imageView = view.findViewById(R.id.iv_toast); TextView textView = view.findViewById(R.id.tv_toast); imageView.setImageResource(R.drawable.icon_smile); textView.setText("自定義Toast"); toastCustom.setView(view); toastCustom.setDuration(Toast.LENGTH_LONG); toastCustom.show();
簡單封裝
public class ToastUtil { public static Toast mToast; public static void showMsg(Context context,String msg){ if(mToast == null){ mToast = Toast.makeText(context,msg,Toast.LENGTH_LONG); }else{ mToast.setText(msg); } mToast.show(); } }
