編輯:關於 Android 程式設計
這裡介紹一套名為 BToast 的自訂提示元件。它的設計目標有兩個:使用方式要夠簡單,以及外觀要能自訂。BToast 內建兩種樣式——純文字,以及帶圖示(提供「√」與「×」兩種)。
它還有一個與原生 Toast 明顯不同的行為:系統 Toast 採用佇列機制,前一個消失後下一個才會出現;BToast 則會直接把目前的提示頂掉,永遠只顯示最新那一則。對使用者來說,反饋會即時很多。
除了版面檔,還需要三個素材:背景、√ 圖示、× 圖示。背景可以直接用 XML 畫出來,不必準備圖片。
toast_back.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" > <corners android:radius="12dp" /> <solid android:color="#CC000000"/> </shape>
√ 與 × 建議使用圖片素材,這裡不再贅述。接著完成提示本體的版面:
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toast_back" android:gravity="center_vertical" android:padding="13dp" android:orientation="vertical" > <ImageView android:id="@+id/toast_img" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/toast_y" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textColor="#FFFFFF" android:gravity="center" android:textSize="17sp" /> </LinearLayout>
先建立 BToast 類別,繼承 Toast 並以靜態成員保存唯一實例:
public class BToast extends Toast {
/** Toast 單例 */
private static BToast toast;
public BToast(Context context) {
super(context);
}
為了做到「新提示頂掉舊提示」,需要覆寫幾個方法,並把例外吞掉以免造成閃退:
/** 隱藏目前的 Toast */
public static void cancelToast() {
if (toast != null) {
toast.cancel();
}
}
public void cancel() {
try {
super.cancel();
} catch (Exception e) { }
}
@Override
public void show() {
try {
super.show();
} catch (Exception e) { }
}
接著把自訂版面灌進 Toast:
private static void initToast(Context context, CharSequence text) {
try {
cancelToast();
toast = new BToast(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast_layout, null);
toast_img = (ImageView) layout.findViewById(R.id.toast_img);
TextView toast_text = (TextView) layout.findViewById(R.id.toast_text);
toast_text.setText(text);
toast.setView(layout);
toast.setGravity(Gravity.CENTER, 0, 70);
} catch (Exception e) {
e.printStackTrace();
}
}
核心的顯示邏輯如下,這裡用三個常數描述圖示狀態,並在顯示圖示時加上 Y 軸旋轉動畫:
/** 不顯示圖示 */
private static final int TYPE_HIDE = -1;
/** 顯示 √ */
private static final int TYPE_TRUE = 0;
/** 顯示 × */
private static final int TYPE_FALSE = 1;
private static void showToast(Context context, CharSequence text, int time, int imgType) {
initToast(context, text);
if (time == Toast.LENGTH_LONG) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
if (imgType == TYPE_HIDE) {
toast_img.setVisibility(View.GONE);
} else {
if (imgType == TYPE_TRUE) {
toast_img.setBackgroundResource(R.drawable.toast_y);
} else {
toast_img.setBackgroundResource(R.drawable.toast_n);
}
toast_img.setVisibility(View.VISIBLE);
ObjectAnimator.ofFloat(toast_img, "rotationY", 0, 360).setDuration(1700).start();
}
toast.show();
}
你可能會注意到這個方法是 private。若直接改成 public 當然也能用,但呼叫端仍得記住一長串參數,跟原生 Toast 比並沒有省事多少。
既然「自訂」的意思就是規則由我們自己定,那不如把介面收斂成幾個好讀的重載方法:
/** 純文字提示 */
public static void showText(Context context, CharSequence text) {
showToast(context, text, Toast.LENGTH_SHORT, TYPE_HIDE);
}
/** 帶圖示的提示,isSucceed 決定顯示對號或叉號 */
public static void showText(Context context, CharSequence text, boolean isSucceed) {
showToast(context, text, Toast.LENGTH_SHORT, isSucceed ? TYPE_TRUE : TYPE_FALSE);
}
/** 純文字提示,可指定持續時間 */
public static void showText(Context context, CharSequence text, int time) {
showToast(context, text, time, TYPE_HIDE);
}
/** 帶圖示的提示,可指定持續時間 */
public static void showText(Context context, CharSequence text, int time, boolean isSucceed) {
showToast(context, text, time, isSucceed ? TYPE_TRUE : TYPE_FALSE);
}
呼叫端因此變得非常直覺:
public void click(View view) {
switch (view.getId()) {
case R.id.btn_text:
BToast.showText(this, "簡單提示");
break;
case R.id.btn_text_true:
BToast.showText(this, "簡單提示 正確圖標", true);
break;
case R.id.btn_text_false:
BToast.showText(this, "簡單提示 錯誤圖標", false);
break;
case R.id.btn_text_long:
BToast.showText(this, "簡單提示 長~", Toast.LENGTH_LONG);
break;
case R.id.btn_text_true_long:
BToast.showText(this, "簡單提示 正確圖標 長~", Toast.LENGTH_LONG, true);
break;
case R.id.btn_text_false_long:
BToast.showText(this, "簡單提示 錯誤圖標 長~", Toast.LENGTH_LONG, false);
break;
}
}
比原生 Toast 好用不少吧?成果如下:
BamboyToast 效果圖

原始碼:BamboyToast_jb51.rar
Launcher 圖標矩形化改造實踐:OpenCV 圖像識別的落地與取捨
一、需求 把 Launcher 上的應用圖標統一改造為矩形形態,效果如下: 二、實現方案 核心思路是引入 OpenCV 做圖像輪廓識別與形狀判斷。坦白說,在接這個需求
Android 性能優化與內存洩漏分析完全指南
性能問題一般可歸納為三類: 1. UI 卡頓與穩定性,這類問題用戶可直接感知,最為重要。 2. 內存問題,主要表現為內存洩漏。若存在洩漏,應用會不斷消耗內存,容易導致頻繁
Android Shape Drawable 形狀資源教學
什麼是 Shape DrawableShape Drawable 是 Android 中常見的形狀資源,可用 XML 定義矩形、圓角、漸層、邊框與內距等樣式。它非常適合用
手機QQ通知欄不顯示圖標怎麼辦 三種原因排查
安卓手機的通知欄可以顯示QQ圖標,但在QQ未退出的情況下卻看不到圖標,這是為什麼?又該怎麼處理?下面我們一起排查。 手機QQ不顯示圖標的原因 原因一:QQ其實已被退出