Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂 Toast 樣式:讓提示訊息更好看也不重複彈出

編輯:關於 Android 開發

自訂 Toast 樣式:讓提示訊息更好看也不重複彈出

系統預設的 Toast 樣子實在朴素,而且連續觸發時容易疊在一起。想換上自訂版面,其實只要四個步驟:

  1. 建立一個 Toast 物件。
  2. 準備一個自訂的 View 作為顯示內容。
  3. 用 setView() 掛上自訂 View,再用 setDuration() 決定顯示時間長短。
  4. 把 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 的用法:

  • gradient:漸層。startColor 與 endColor 分別是起始色與結束色,centerColor 是中段色。
  • android:angle 是漸層角度,必須是 45 的整數倍。

最後只要一行呼叫即可:

MyToast.showCustomerToastLong(this, "測試訊息框");

大功告成。本文謝絕轉載,程式猿不容易……

Created by hexiang on 2014/12/10. 清分依然

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved