自定义Toast,有图标,有文字,不会重复显示

本文介绍了一种自定义Toast的方法,包括加入图标、文字,并具备防止重复显示的功能。提供了完整的Java代码实现,以及对应的布局和调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前几天写了一个自定义Toast,加入了图标,文字,多次点击不会重复显示等功能。

显示效果如图所示:
自定义Toast效果图

下面开始贴代码:

java代码

public class ToastCancel {

    public static ToastCancel toastCancel;

    private static View view;
    private static Toast toast;
    private static TextView text;
    private static ImageView img;

    private ToastCancel() {
    }

    public static ToastCancel getInstance(Context context) {
        if (toast == null) {
            toastCancel = new ToastCancel();

            view = LayoutInflater.from(context).inflate(R.layout.toast_cancel, null);
            text = (TextView) view.findViewById(R.id.text_toast);
            img = (ImageView) view.findViewById(R.id.img_toast);

            toast = new Toast(context);
            toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
            toast.setDuration(Toast.LENGTH_SHORT);  // Toast显示的时间
            toast.setView(view);                    // 设置Toast布局
        }
        return toastCancel;
    }

    /**
     * 显示
     */
    public void show(int drawable, String str) {
        text.setText(str); // 设置显示文字
        img.setImageResource(drawable);//设置显示图标
        toast.show();
    }

    public void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }
}

布局代码

<?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="horizontal">

    <LinearLayout
        android:id="@+id/toast_emailOne_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toast_bg"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="4dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingTop="4dp">

        <ImageView
            android:id="@+id/img_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/offline" />

        <TextView
            android:id="@+id/text_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="网络异常,请检查网络环境"
            android:textColor="#ffffff"
            android:textSize="25sp" />
    </LinearLayout>
</LinearLayout>

drawable文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#77000000"></solid>
    <corners android:radius="20dp"></corners>
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"></padding>

</shape>

调用方式

ToastCancel.getInstance(MainActivity.this).show(R.mipmap.file, "我是自定义Toast");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值