Android自定义Toast

Android自定义Toast

一、创建Toast布局文件layout_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/shape_corner_toast">
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp_19"
    android:layout_marginRight="@dimen/dp_19"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="@dimen/dp_11"
    android:paddingBottom="@dimen/dp_11">
    <ImageView
      android:id="@+id/toast_custom_iv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:src="@mipmap/ic_launcher" />

    <TextView

      android:id="@+id/toast_custom_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="@dimen/dp_7"
      android:textColor="#FFFFFF"
      android:textSize="@dimen/sp_7"
      tools:text="点击toast" />
  </LinearLayout>
</LinearLayout>

二、封装Toast

/**
 * Toast类
 */
public class ToastUtils {

  private static ToastUtils toastUtils = null;
  private Toast toast = null;
  private TextView toastMsg;
  private int imId;

  private ToastUtils() {

  }

  public static ToastUtils getInstance() {
    if (toastUtils == null) {
      synchronized (ToastUtils.class) {
        if (toastUtils == null) {
          toastUtils = new ToastUtils();
        }
      }
    }
    return toastUtils;
  }

  public void showToast(Context context, String msg) {
    if (toast == null) {
      toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
    } else {
      toast.setText(msg);
      toast.setDuration(Toast.LENGTH_SHORT);
    }
    toast.show();
  }

  public void showToast2(Context context, String msg) {
    if (toast == null) {
      toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    } else {
      toast.setText(msg);
      toast.setDuration(Toast.LENGTH_LONG);
    }
    toast.show();
  }

  public void showToastLong(Context context, String msg, int im_id) {

    toast = new Toast(context);
    View view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
    toastMsg = (TextView) view.findViewById(R.id.toast_custom_tv);
    ((ImageView) view.findViewById(R.id.toast_custom_iv)).setImageResource(im_id);
    toastMsg.setText(msg);
    toast.setView(view);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);

    toast.show();
  }

//	public void showToastShort(Context context, String msg) {
//		if (toast == null || toast.getView() == null) {
//			toast = new Toast(context);
//			View view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
//			toastMsg = (TextView) view.findViewById(R.id.toastMsg);
//			toastMsg.setText(msg);
//			toast.setView(view);
//			toast.setDuration(Toast.LENGTH_SHORT);
//		} else {
//			toastMsg.setText(msg);
//			toast.setDuration(Toast.LENGTH_SHORT);
//		}
//		toast.show();
//	}

}

三、调用

1、普通调用

ToastUtils.getInstance().showToast(LoginActivity.this, "网络错误~");

2、自定义调用

ToastUtils.getInstance()
        .showToastLong(LoginActivity.this, "登录成功",
            R.drawable.sucess_icon);
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值