Android Toast 簡單分析

AguedaNcx 9年前發布 | 5K 次閱讀 Android開發 移動開發 TOAST

介紹

A toast provides simple feedback about an operation in a small popup. It only fills the amount of

space required for the message and the current activity remains visible and interactive. For

example, navigating away from an email before you send it triggers a “Draft saved” toast to let

you know that you can continue editing later. Toasts automatically disappear after a timeout.

翻譯:

Toast以一種小彈框的方式來給予用戶反饋,它只需要消息可以顯示出來的那小部分空間,同時Activity依

然可見可交互。例如,當你寫郵件的時候退出,會觸發“草稿已保存”的Toast來讓你知道你以后可以繼續

編輯這封郵件。Toast會在一段時間后自己消失。

類結構

Toast的成員

成員解釋

成員 含義
Toast(Context) 構造函數
show() 顯示Toast
cancel() 取消Toast
setMargin(float,float) 設置橫向的margin和縱向的margin
setGravity(int ,int, int) 設置toast的位置重心和相對的XOffset, YOffset
makeText(Context, CharSequence, int) 利用字符串創建Toast
makeText(Context, int, int) 利用字符串資源創建Toast
setView(View) 設置自定義Toast顯示的View
setDuration(int) 設置Toast的顯示時間
setText 設置Message文本內容
備注: 部分getter方法和setter方法對應,沒有寫入

實際使用

布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

<Button
    android:id="@+id/bt_simple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:text="Simple Toast" />

<Button
    android:id="@+id/bt_custom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:background="@color/colorPrimary"
    android:text="Custom Toast" />

</LinearLayout></code></pre>

自定義的Toast布局

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/ic_account_circle_black_24dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center"
    android:text="This is a Custom Toast !"
    android:textStyle="italic" />

</LinearLayout></code></pre>

代碼內容

package mraz.com.toastdemo;

import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Context mContext;
Button btSimple, btCustom;

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

    mContext = this;

    btSimple = (Button) findViewById(R.id.bt_simple);
    btCustom = (Button) findViewById(R.id.bt_custom);

    btSimple.setOnClickListener(this);
    btCustom.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.bt_simple:
            Toast.makeText(mContext, " This is a simple toast", Toast.LENGTH_SHORT).show();
            break;
        case R.id.bt_custom:
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            View custToast = layoutInflater.inflate(R.layout.custom_toast, null);
            Toast toast = new Toast(mContext);
            toast.setView(custToast);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.show();
            break;
    }
}

}</code></pre>

實際效果

備注

Toast的內容比較簡單,但是開發過程中應該會經常用到,說不定偶爾還會碰到顯示不出來的情況,不要忘記執行show()方法,當然,還有可能是其他的原因導致你的Toast無法顯示~

 

來自:http://blog.csdn.net/poorkick/article/details/51842700

 

 本文由用戶 AguedaNcx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!