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

Android Toast 五大用法整理:樣式、自訂版面與跨執行緒

編輯:關於 Android 程式設計

Toast 大家都不陌生,這裡不囉嗦,直接上圖上程式碼,把常見的五種用法一次收齊。

版面配置:五顆按鈕對應五種用法

main.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dip">

    <Button android:id="@+id/btnSimpleToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="預設" />

    <Button android:id="@+id/btnSimpleToastWithCustomPosition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自訂顯示位置" />

    <Button android:id="@+id/btnSimpleToastWithImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="帶圖片" />

    <Button android:id="@+id/btnCustomToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="完全自訂" />

    <Button android:id="@+id/btnRunToastFromOtherThread"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="其他執行緒" />

</LinearLayout>

自訂版面:custom.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/llToast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvTitleToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dip"
        android:background="#bb000000"
        android:gravity="center"
        android:text="Attention"
        android:textColor="#ffffffff" />

    <LinearLayout
        android:id="@+id/llToastContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:background="#44000000"
        android:orientation="vertical"
        android:padding="15dip">

        <ImageView
            android:id="@+id/tvImageToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/tvTextToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textColor="#ff000000" />

    </LinearLayout>

</LinearLayout>

程式碼:五種呼叫方式

public class MainActivity extends Activity implements OnClickListener {

    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.btnSimpleToast).setOnClickListener(this);
        findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);
        findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
        findViewById(R.id.btnCustomToast).setOnClickListener(this);
        findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast toast;
        switch (v.getId()) {
            // 1. 最簡單的用法
            case R.id.btnSimpleToast:
                Toast.makeText(getApplicationContext(), "預設 Toast 樣式", Toast.LENGTH_SHORT).show();
                break;

            // 2. 自訂顯示位置
            case R.id.btnSimpleToastWithCustomPosition:
                toast = Toast.makeText(getApplicationContext(), "自訂位置 Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;

            // 3. 在既有 Toast 上追加圖片
            case R.id.btnSimpleToastWithImage:
                toast = Toast.makeText(getApplicationContext(), "帶圖片的 Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                LinearLayout toastView = (LinearLayout) toast.getView();
                ImageView imageCodeProject = new ImageView(getApplicationContext());
                imageCodeProject.setImageResource(R.drawable.ic_launcher);
                toastView.addView(imageCodeProject, 0);
                toast.show();
                break;

            // 4. 完全自訂版面
            case R.id.btnCustomToast:
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom,
                        (ViewGroup) findViewById(R.id.llToast));
                ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
                image.setImageResource(R.drawable.ic_launcher);
                TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
                title.setText("Attention");
                TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
                text.setText("完全自訂 Toast");
                toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
                break;

            // 5. 從其他執行緒彈出
            case R.id.btnRunToastFromOtherThread:
                new Thread(new Runnable() {
                    public void run() {
                        showToast();
                    }
                }).start();
                break;
        }
    }

    private void showToast() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "我來自其他執行緒!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

幾個容易踩到的細節

  • Toast 只能在主執行緒(UI 執行緒)操作,背景執行緒請用 Handler 切回來,這也是第五種示範的重點。
  • setGravity 只在系統預設版面與自訂版面上都有效,offset 的正負方向在不同 API 上略有差異,實機確認最保險。
  • 想修改既有 Toast 的內容(例如加圖片),可先 getView() 取得它的 LinearLayout 再 addView;但這種做法依賴系統內部實作,相容性不如直接自訂版面。
  • 同一時間畫面上只會有一個 Toast,新的會把舊的頂掉,因此不適合當作長時間的狀態提示。
  • 顯示时长只有 SHORT 與 LONG 兩種,自訂秒數無效。

執行專案即可看到五種效果。

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