建立普通類,并建立對應的方法
建立Toast實體類;
根據上下文加載布局界面,可以是任意定義的xml布局檔案,
也可以是利用java代碼編寫的布局
設定Toast的相應屬性,例如對齊方式gravity,顯示時長Duration等
【注意】在建立的類下的方法中,若是通過setView方法設定Toast的
顯示界面,若需設定Toast的text,還需通過原始的方法建立對象
但這樣會損失自定義的概念,不建議這樣使用
public class MyToast {
public static void showT(Context context,String text){
Toast toast=new Toast(context);
View view=View.inflate(context, R.layout.toast_normal_show_layout, null);
TextView tv=(TextView) view.findViewById(R.id.toast_show_text);
tv.setText(text);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
}
調用方法:在需要調用的類中建立出自定義的類,根據需要傳入參數即可
String toastMsg="共結束記憶體.經常清理可提升運作速度!";
MyToast toast=new MyToast();
toast.showT(getBaseContext(), toastMsg);