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

Android 開發系列(二十三):打造帶圖示的自定義 Toast 提示框

編輯:關於 Android 程式設計

Toast 是 Android 中最常見的輕量提示元件,但預設樣式只是一行純文字。若想讓提示更具辨識度,可以放進圖示,甚至完全自定義版面。

實作起來並不難,核心只有兩步:

  1. 定義一個自訂的 Layout 視圖。
  2. 用 Toast.setView() 把它設給 Toast 顯示。

這裡的做法是用 LinearLayout 當容器,裡面放一個 ImageView 加一個 TextView。

一、建立版面配置

先在一個 Android 專案中編輯 main.xml,放兩個按鈕分別觸發兩種樣式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal" >

    <Button
        android:id="@+id/simple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示簡單提示" />

    <Button
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示帶圖片的提示" />
</LinearLayout>

二、撰寫程式邏輯

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ToastTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 一般樣式
        Button simple = (Button) findViewById(R.id.simple);
        simple.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View source) {
                Toast toast = Toast.makeText(ToastTest.this,
                        "簡單的提示信息", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        // 自定義樣式
        Button bn = (Button) findViewById(R.id.bn);
        bn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View source) {
                Toast toast = new Toast(ToastTest.this);
                toast.setGravity(Gravity.CENTER, 0, 0); // 設定顯示位置

                // 建立圖示
                ImageView image = new ImageView(ToastTest.this);
                image.setImageResource(R.drawable.tools);

                // 建立文字
                TextView textView = new TextView(ToastTest.this);
                textView.setText("帶圖片的提示信");
                textView.setTextSize(30);
                textView.setTextColor(Color.MAGENTA);

                // 組裝容器
                LinearLayout ll = new LinearLayout(ToastTest.this);
                ll.addView(image);
                ll.addView(textView);

                toast.setView(ll);                       // 套用自定義視圖
                toast.setDuration(Toast.LENGTH_LONG);    // 設定顯示時間
                toast.show();
            }
        });
    }
}

執行結果如下:

實務上的幾點提醒

  • 用 setView 後就不能再用 setText:setView() 與 setText() 是兩套互斥的呈現方式,混用會拋出異常。
  • 位置由 setGravity 決定:自定義視圖時,位置不再由版面配置檔控制,而是完全由 setGravity() 決定。
  • duration 只有兩種有效值:LENGTH_SHORT 與 LENGTH_LONG。雖然 setDuration() 接受任意毫秒數,但系統會忽略自訂值。
  • Toast 是執行緒安全的:可以在子執行緒呼叫,這點和 Dialog 不同。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved