Android自带的toast样式不太好看,UI妹子可能会自己设计一个全局的提示,如图:
这时候就需要用到CustomView和toastUtil工具类了
一 : customView:
public class CustomToast extends Toast {
TextView textView;
/**
* Construct an empty Toast object. You must call {@link #setView} before you
* can call {@link #show}.
*
* @param context The context to use. Usually your {@link Application}
* or {@link Activity} object.
*/
public CustomToast(Context context) {
super(context);
init(context);
}
private void init(Context context) {
View view = View.inflate(context, R.layout.layout_toast,null);
textView = (TextView) view.findViewById(R.id.content);
setView(view);
}
@Override
public void setText(CharSequence sequence){
textView.setText(sequence);
}
@Override
public void setText(int resId) {
textView.setText(resId);
}
}
二 :ToastUtil
public class ToastUtil extends Toast{
private static String oldMsg;
protected static Toast toast = null;
private static long oneTime = 0;
private static long twoTime = 0;
/**
* Construct an empty Toast object. You must call {@link #setView} before you
* can call {@link #show}.
*
* @param context The context to use. Usually your {@link Application}
* or {@link Activity} object.
*/
public ToastUtil(Context context) {
super(context);
}
public static void showToast(Context context, int resId) {
showToast(context, context.getString(resId));
}
public static void showToast(Context context, int resId, int gravity) {
showToast(context, context.getString(resId), gravity, 0, 0);
}
public static void showToast(Context context, String s, int gravity) {
showToast(context, s, gravity, 0, 0);
}
public static void showToast(Context context, int resId, int gravity, int offX, int offY) {
showToast(context, context.getString(resId), gravity, offX, offY);
}
public static void showToast(Context context, String s) {
if (TextUtils.isEmpty(s)){
return;
}
if (toast == null) {
toast = new CustomToast(context);
toast.setText(s);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
oneTime = System.currentTimeMillis();
} else {
twoTime = System.currentTimeMillis();
if (s.equals(oldMsg)) {
if (twoTime - oneTime > Toast.LENGTH_SHORT) {
toast.show();
}
} else {
oldMsg = s;
toast.setText(s);
toast.show();
}
}
oneTime = twoTime;
}
public static void showToast(Context context, String s, int gravity, int offX, int offY) {
if (toast == null) {
toast = new CustomToast(context);
toast.setText(s);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(gravity, offX, offY);
toast.show();
oneTime = System.currentTimeMillis();
} else {
twoTime = System.currentTimeMillis();
if (s.equals(oldMsg)) {
if (twoTime - oneTime > Toast.LENGTH_SHORT) {
toast.show();
}
} else {
oldMsg = s;
toast.setText(s);
toast.show();
}
}
oneTime = twoTime;
}
}
三:自定义样式Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_anjian_toast"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="30dp"
android:textColor="#fff"
android:textSize="18dp" />
</LinearLayout>