編輯:關於 Android 程式設計
一、先看源碼在搞什麼
Toast 有兩種顯示方式。最常見的是 Toast.makeText(),源碼如下:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView) v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
而 transient_notification 這個系統版面長這樣:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/toastFrameBackground">
<TextView
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="@color/bright_foreground_dark"
android:shadowColor="#BB000000"
android:shadowRadius="2.75" />
</LinearLayout>
所以想改文字樣式,本質上就是改根版面與那個 id 為 message 的 TextView。第二種方式則是 new 一個 Toast 物件,自己呼叫 setView() 塞入自訂版面,這時自然不會載入 transient_notification。
二、封裝 ToastUtil
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ToastUtil {
private Toast toast;
private LinearLayout toastView;
/** 沿用系統原樣式的 Toast */
public ToastUtil() {
}
/** 完全自訂版面的 Toast */
public ToastUtil(Context context, View view, int duration) {
toast = new Toast(context);
toast.setView(view);
toast.setDuration(duration);
}
/** 在 Toast 中插入自訂 view */
public ToastUtil addView(View view, int postion) {
toastView = (LinearLayout) toast.getView();
toastView.addView(view, postion);
return this;
}
/** 設定文字與背景顏色 */
public ToastUtil setToastColor(int messageColor, int backgroundColor) {
View view = toast.getView();
if (view != null) {
TextView message = ((TextView) view.findViewById(android.R.id.message));
message.setBackgroundColor(backgroundColor);
message.setTextColor(messageColor);
}
return this;
}
/** 設定文字顏色與背景圖 */
public ToastUtil setToastBackground(int messageColor, int background) {
View view = toast.getView();
if (view != null) {
TextView message = ((TextView) view.findViewById(android.R.id.message));
message.setBackgroundResource(background);
message.setTextColor(messageColor);
}
return this;
}
/** 短暫顯示 */
public ToastUtil Short(Context context, CharSequence message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
public ToastUtil Short(Context context, int message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/** 長時間顯示 */
public ToastUtil Long(Context context, CharSequence message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
public ToastUtil Long(Context context, int message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/** 自訂顯示時間 */
public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, duration);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
public ToastUtil Indefinite(Context context, int message, int duration) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, duration);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
public ToastUtil show() {
toast.show();
return this;
}
public Toast getToast() {
return toast;
}
}
三、改背景色
ToastUtil toastUtil = new ToastUtil();
toastUtil.Short(MainActivity.this, "自定義message字體、背景色")
.setToastColor(Color.WHITE, getResources().getColor(R.color.colorAccent))
.show();

修改 Toast 背景色
純色方塊看起來有點呆板,我自己寫了一個名為 toast_radius.xml 的圓角背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的顏色 -->
<solid android:color="#ffc107" />
<!-- android:radius 為圓角半徑 -->
<corners android:radius="20dip" />
</shape>
呼叫時改成:
toastUtil.Short(MainActivity.this, "自定義message字體顏色和背景")
.setToastBackground(Color.WHITE, R.drawable.toast_radius)
.show();

換成圓角背景的 Toast
四、加上圖示
官方認為 Toast 與 Snackbar 都應該只放短文字、不該有圖示,但加上去確實有趣多了:
ImageView toastImage = new ImageView(getApplicationContext());
toastImage.setImageResource(R.mipmap.ic_launcher);
toastUtil.Short(MainActivity.this, "向Toast添加了一個ImageView")
.setToastBackground(Color.WHITE, R.drawable.toast_radius)
.addView(toastImage, 0)
.show();

加入圖示的 Toast
五、完全自訂版面
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.image, null); new ToastUtil(MainActivity.this, view, Toast.LENGTH_SHORT).show();

自訂版面的 Toast,版面裡只有一個使用預設圖示的 ImageView
六、避免連續觸發時排隊
連續呼叫 show() 時,Toast 會一個接一個排隊顯示,體驗其實不太好。所以我在 Short / Long 裡都先判斷 toast 是否尚未建立、或是否被加進了額外的 view:若是就重新產生一個 Toast 物件,否則只更新文字與顯示時間,讓提示永遠停留在最新一則。

改寫版面後的 Toast,不再排隊顯示
總結
這個工具類並非最終形態,實際上請依專案需求再調整。以上就是 Android Toast 的進階用法,有興趣的朋友動手試試看吧。
Android 遊戲實戰:打飛機遊戲的開始選單頁
這個系列的最終目標是做出可以操作的打飛機:控制小飛機左右移動、閃避子彈、擊倒 Boss。本文完成第一步——開始選單介面。 準備工作 先把圖片資源整個複製進來。接著,任何一
Android 圖片緩存系列:Bitmap 與 BitmapFactory 詳解(一)
準備研究圖片緩存框架之前,先把基礎打牢更為妥當。本篇聚焦兩個最核心的類:Bitmap 與 BitmapFactory。 Bitmap 類 Bitmap 是 Android
Android 模擬器上開發與測試 NFC 應用的實例詳解
NFC 從 Android 2.3 起獲得支持,但它有一個惱人的限制:只能在帶 NFC 芯片的真機上開發測試,且傳輸測試往往需要兩部支持 NFC 的手機,讀寫標簽還需準備
舊手機變身電腦無線耳機/喇叭:SoundWire使用指南
SoundWire可把Android手機變成PC的無線聲卡或耳機。先在PC安裝伺服器端並記下Server Address,手機與PC連上同一Wi-Fi後開啟App搜尋或手