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