Android 自定义带图标Toast

背景资源

1、<color name="numtext">#999999</color>

2、

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 设置透明背景色 -->
    <solid android:color="@color/numtext" />

    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="@color/numtext" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:radius="8dp"/>
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="7dp"
        android:left="9dp"
        android:right="9dp"
        android:top="7dp" />

</shape>

3、layout.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">

    <!-- android:minHeight="80dp"-->
    <LinearLayout
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@drawable/backtoa"

        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dp">
        <!--android:background="@drawable/toast_bg"-->
        <ImageView
            android:id="@+id/toast_image"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:background="@mipmap/em_dx_checkbox_on" />

        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:text="保存成功"
            android:textColor="#ffffff"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

4、工具类

/**
 * 作者:created by meixi
 * 邮箱:[email protected]
 * 日期:2018/8/28 08
 */

public class ToastUtils {

    private static Context mContext ;

    public static void showToast(String toast,Context context) {
        mContext = context;
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    /**
     *  带图片的吐司提示
     * @param text
     */
    public static void showCustomImgToast(String text ,Context context) {
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     *  带图片的吐司提示
     *  通过参数传递,可是设置吐司的图片和文字内容
     * @param text
     */
    public static void showCustomImgToast(String text,int imgResId) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     *  不带图片的吐司提示
     * @param text
     */
    public static void showCustomToast(String text) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setVisibility(View.GONE);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     * 带图片的吐司,设置吐司弹出的位置为屏幕中心
     * @param text
     */
    public static void showCustomToastCenter(String text ,Context context) {
        showCustomToastCenter(text, R.mipmap.em_dx_checkbox_on,context);
    }

    /**
     * 带图片的吐司,设置吐司弹出的位置为屏幕中心
     * 通过参数传递,可是设置吐司的图片和文字内容
     * @param text
     */
    public static void showCustomToastCenter(String text, int imgResId,Context context) {
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(imgResId);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.setGravity(Gravity.BOTTOM, 0, 50);
        toast.show();
    }
}

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/82142409