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

Android 學習筆記:自訂樣式 Toast 提示框

編輯:關於 Android 程式設計

系統預設的 Toast 只能顯示一行純文字,樣子單調。如果想放圖示、換底色或做更複雜的排版,就得自己指定版面。以下示範完整做法。

1. 準備 Toast 專用的版面檔

新增 res/layout/toast.xml,用 LinearLayout 把圖示與文字排成一列:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="8dp"
    android:background="#DAA">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/in" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp" />
</LinearLayout>

2. 在 Activity 中套用

package com.lc.toastdialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button button1;
    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) this.findViewById(R.id.button1);
        button2 = (Button) this.findViewById(R.id.button2);

        // 一般 Toast
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "普通的 Toast",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // 自訂 Toast
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 載入自訂版面
                View layout = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.toast, null);

                // 設定文字內容
                TextView textView = (TextView) layout.findViewById(R.id.text);
                textView.setText("hello world!");

                Toast toast = new Toast(MainActivity.this);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

效果

自訂 Toast 的三個關鍵步驟:用 LayoutInflater.inflate 取得版面物件、呼叫 setView 把它交給 Toast、最後記得 show()。位置則由 setGravity 決定。

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