欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义Toast

程序员文章站 2022-08-29 12:59:37
Android自定义Toast一、创建Toast布局文件layout_toast.xml

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);

本文地址:https://blog.csdn.net/weixin_42345592/article/details/109380286

相关标签: android