Android自定义Toast

   日期:2020-11-01     浏览:93    评论:0    
核心提示: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自定义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


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);
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服