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

Android Toast 樣式與自訂 Toast 完整實作

編輯:關於 Android 程式設計

一、自訂 Toast 的配置檔

先準備一個 toast_layout.xml,用一般的 Layout 描述想呈現的樣子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout"
    android:layout_width="200dip"
    android:layout_height="fill_parent"
    android:background="#f0f0f0"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:text="@string/toast_text_1"
        android:textColor="#ffffff"
        android:textSize="20dip" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#999999"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip"
            android:src="@drawable/icon_message_nub" />

        <TextView
            android:id="@+id/txt_context"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:text="@string/toast_text_2"
            android:textColor="#ffffff"
            android:textSize="15dip" />
    </LinearLayout>
</LinearLayout>

二、Activity 中的四種樣式

public class ToastActivity extends Activity implements OnClickListener {

    private Button style1btn, style2btn, style3btn, style4btn;
    private Toast toast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_main);
        initViews();
        initListeners();
    }

    private void initViews() {
        style1btn = (Button) findViewById(R.id.style1_btn);
        style2btn = (Button) findViewById(R.id.style2_btn);
        style3btn = (Button) findViewById(R.id.style3_btn);
        style4btn = (Button) findViewById(R.id.style4_btn);
    }

    private void initListeners() {
        style1btn.setOnClickListener(this);
        style2btn.setOnClickListener(this);
        style3btn.setOnClickListener(this);
        style4btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder;
        AlertDialog dialog;

        switch (v.getId()) {

            // 樣式一:系統預設
            case R.id.style1_btn:
                Toast.makeText(ToastActivity.this, "系統默認的 Toast 樣式",
                        Toast.LENGTH_LONG).show();
                break;

            // 樣式二:改變顯示位置
            case R.id.style2_btn:
                toast = Toast.makeText(ToastActivity.this, "改變 Toast 顯示位置的樣式",
                        Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);   // 置中
                toast.show();
                break;

            // 樣式三:自訂版面(含圖片)
            case R.id.style3_btn:
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.my_custom_toast,
                        (ViewGroup) findViewById(R.id.toast_layout));

                toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(view);
                toast.show();
                break;

            // 樣式四:用 Dialog 取代 Toast
            case R.id.style4_btn:
                LayoutInflater inflater1 = getLayoutInflater();
                View styleView = inflater1.inflate(R.layout.my_custom_toast,
                        (ViewGroup) findViewById(R.id.toast_layout));
                TextView tv = (TextView) styleView.findViewById(R.id.txt_context);
                tv.setText("長時間顯示的 Toast 其實就是一個自訂的 Dialog!");

                builder = new AlertDialog.Builder(this);
                builder.setView(styleView);
                dialog = builder.create();
                dialog.show();
                break;

            default:
                break;
        }
    }
}

四種做法的適用場合

樣式一(預設) 最簡單,位置由系統決定,適合一般提示。

樣式二(改位置) 透過 setGravity 調整,常用於把提示放在畫面上特定區域,避免遮住重要內容。

樣式三(自訂 view) 自由度最高,可以放圖示、多行文字、自訂配色。注意這裡是用 new Toast(Context) 搭配 setView,而非 Toast.makeText。

樣式四(改用 Dialog) 註解說「長時間顯示的 Toast 其實就是一個自訂的 Dialog」,這句話點出了 Toast 的根本限制。

關於 Toast 的重要限制

Toast 的顯示時長只有兩種:LENGTH_SHORT 與 LENGTH_LONG。傳入自訂的毫秒數在 API 30 之前不會有任何效果(系統會自行換算成 2 秒或 3.5 秒)。若真的需要長時間顯示的提示,正確做法就是樣式四——直接用 Dialog,而不是想辦法硬掰 Toast。

自訂 Toast 在 Android 7.1(API 25)之後被大幅限制。從這個版本起,使用 setView() 的自訂 Toast 會被系統改以文字模式的 Toast 呈現,也就是說樣式三在新版裝置上會失效。這是官方基於安全考量的變更——自訂 view 意味著 App 可以在任何畫面上渲染任意內容,容易被濫用於釣魚與惡意覆蓋。官方建議改用 Snackbar 或 Notification 來取代需要自訂外觀的提示。

因此,樣式一、二、四在今日仍然完全可用;樣式三則僅適合維護舊版 App 時參考,新專案不應再採用。

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