現在很多的應用 都會自定義個Toast.的方式
Toast 本身的代碼是很長的,有時間可以看看沒有 時間 可以不看 。Toast 的兩個方法是自定義Toast的重點 一個就是 setView(),setGravity(); 有這兩個就夠了 一個是 設置View 一個是設置位置 這樣的話 你就可以通過 這兩個方法的組合進行 好看的Toast。
我選擇的是繼承Toast 重新定義了一個自己的Toast 比較簡單 貼代碼吧,根據這一層可以再進一步的封裝 ,可以在子線程中 直接調用使用。 new Handler(Looper.getMainLooper); 這樣就獲取到主線程的handler 進行通信。自己封裝吧。
public final class CustomToast extends Toast {
public TextView tipInfo;
public ImageView tipImage;
public MCCustomToast(Context context) {
super(context);
View view = LayoutInflater.from(context).inflate(R.layout.toast_style_center, null);
this.setView(view);
this.tipInfo = (TextView) view.findViewById(R.id.jd_toast_txt);
this.tipImage = (ImageView) view.findViewById(R.id.jd_toast_image);
this.setGravity(17, 0, 0);
}
public final void setType(int type) {
if (this.tipImage != null) {
switch (type) {
case 1: {
this.tipImage.setBackgroundResource(R.drawable.toast_exclamation);
return;
}
case 2: {
this.tipImage.setBackgroundResource(R.drawable.toast_tick);
return;
}
}
}
}
public final void setText(CharSequence text) {
if (this.tipInfo != null) {
this.tipInfo.setText(text);
}
}
}