让一个Activity唯有一个自定义Toast
让一个Activity只有一个自定义Toast
package com.wxlh.ptas.ui; import android.content.Context; import android.view.Gravity; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.wxlh.ptas.R; public class TextToast { private Context context; private Toast toast; public TextToast(Context context) { this.context = context; toast = new Toast(context); } private Toast getToast() { if (null == toast) { toast = new Toast(context); } return toast; } public void show(Context context, String msg) { Toast toast = getToast(); TextView textOverlay = new TextView(context); LinearLayout mLinearLayout = new LinearLayout(context); mLinearLayout.setBackgroundResource(R.drawable.bg_hitchar); textOverlay.setText(msg); textOverlay.setTextColor(R.color.white); textOverlay.setTextSize(70); textOverlay.setWidth(100); textOverlay.setMinWidth(100); textOverlay.setPadding(10, 10, 10, 10); textOverlay.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL); mLinearLayout.addView(textOverlay); toast.setView(mLinearLayout); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } }
调用:
1. private TextToast textToast; 2.textToast = new TextToast(this); 3.textToast.show(DailContact.this, element.substring(0, 1));