編輯:關於 Android 開發
系統預設的 Toast 樣子實在朴素,而且連續觸發時容易疊在一起。想換上自訂版面,其實只要四個步驟:
下面是完整實作:

package com.android.hexiang.otptoken;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
import com.android.hexiang.otptoken.R;
/**
* Created by hexiang on 2014/12/10.
*/
public class MyToast {
private static Toast mToast = null;
/**
* 建立自訂訊息框
*/
@SuppressWarnings("deprecation")
private static Toast showCustomerToast(final Context c, String msg, int duration) {
// 取得 LayoutInflater,負責把版面檔轉成 View 物件
LayoutInflater inflater = LayoutInflater.from(c);
// 將版面檔轉成對應的 View
View layout = inflater.inflate(R.layout.my_customer_toast_layout, null);
layout.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.back_toast_style));
// 從 layout 中依 id 找出 TextView
TextView textView = (TextView) layout.findViewById(R.id.txt_msg);
textView.setText(msg);
// 建立 Toast 物件並設定參數
Toast toast = new Toast(c.getApplicationContext());
toast.setDuration(duration);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 160);
toast.setView(layout);
return toast;
}
/**
* 長時間顯示的訊息框
*/
@SuppressWarnings("deprecation")
public static void showCustomerToastLong(final Context c, String msg) {
// 避免重複彈出:已有實例時只更新內容
if (mToast == null) {
mToast = showCustomerToast(c, msg, Toast.LENGTH_LONG);
} else {
TextView txt_msg = (TextView) mToast.getView().findViewById(R.id.txt_msg);
txt_msg.setText(msg);
mToast.setDuration(Toast.LENGTH_LONG);
}
mToast.show();
}
/**
* 短時間顯示的訊息框
*/
@SuppressWarnings("deprecation")
public static void showCustomerToastShot(final Context c, String msg) {
if (mToast == null) {
mToast = showCustomerToast(c, msg, Toast.LENGTH_SHORT);
} else {
TextView txt_msg = (TextView) mToast.getView().findViewById(R.id.txt_msg);
txt_msg.setText(msg);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
}
接著是版面檔 my_customer_toast_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toast_layout_root"
android:orientation="vertical" >
<TextView
android:layout_margin="6dp"
android:layout_gravity="center_vertical|center_horizontal"
android:id="@+id/txt_msg"
android:layout_width="match_parent"
android:textColor="@color/white_light"
android:textSize="14sp"
android:text="this is a test toast"
android:layout_height="wrap_content" />
</LinearLayout>
再來是自訂的漸層背景 back_toast_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
<gradient
android:angle="315"
android:centerX="0.5"
android:centerY="0.5"
android:centerColor="#fcdede"
android:endColor="#e5edbe"
android:startColor="#d2b2b2" />
<corners android:radius="3dp" />
</shape>
順手複習一下 shape 的用法:
最後只要一行呼叫即可:
MyToast.showCustomerToastLong(this, "測試訊息框");
大功告成。本文謝絕轉載,程式猿不容易……
Created by hexiang on 2014/12/10. 清分依然
Android 掃掃圖書:條碼查書與讀書筆記的入門級 App 源碼
「書,是人類的靈魂。」——先容許我把這句略顯肉麻的文案放在開頭,因為它剛好說明了這個小專案想做的事:幫你找到那本對的書。 這個 App 想解決什麼 在書店或朋友書架上翻
Android 自定義下拉菜單:容器與內容分離
Android 自定義下拉菜單:容器與內容分離 Android 自定義下拉菜單:容器與內容分離在 Android 開發中,類似美團首頁的下拉彈出菜單經常會用到。不同項目對
Android 自定義控制項實作:城市選擇器完整教學
Android 自定義控制項——城市選擇本文一步步實作一個實用的城市選擇控制項,涵蓋自定義組合控制項、右側字母索引欄、以及本地 JSON 資料的解析與渲染。實現效果:&n
網上商城實戰(四):首頁二級分類的查詢
網上商城實戰(四):首頁二級分類的查詢 1. 創建表 CREATE TABLE `categorysecond` ( `csid` int(11) NOT NULL A