Android自定义Toast

本文介绍了如何在Android中自定义Toast,包括使用Toast的setView()方法,创建自定义布局文件,封装CustomToast类,实现单例模式,以及提供取消显示和显示新内容的功能。最后展示了自定义Toast的效果和完整代码。

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

官方封装的Toast

Toast.makeText(this, "提示", Toast.LENGTH_SHORT).show;

自定义Toast

一、思路:使用Toast对象的setView()方法
二、实现
1、首先准备一个Toast的布局文件toast_msg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout"
    android:orientation="vertical"
    android:background="@drawable/toast_bg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/toast_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:paddingLeft="10dp"
        android:layout_marginRight="10dp"
        android:text=""
        android:textColor="#ffffff"/>
</LinearLayout>

Toast的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<!--    背景渐变-->
    <gradient
        android:angle="-90"
        android:endColor="#330000ff"
        android:startColor="#3300ff00"/>
    <corners android:radius="5dp"/>
<!--    边框-->
    <stroke
        android:width="1dp"
        android:color="#eeeeee"/>
</shape>

2、封装一个CustomToast类对象,通过单例模式获取。

private static CustomToast instance;

public static CustomToast getInstance(){
        if (instance == null){
            synchronized (CustomToast.class){
                if (instance == null){
                    instance = new CustomToast();
                }
            }
        }
        return instance;
    }

3、编写一个cancle()方法,每次显示新的内容时,就取消显示上次的

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

4、自定义布局

private final int MARGIN_DP = 200;
/**
     * 显示自定义布局
     * @param context 上下文对象
     * @param msg   显示内容
     * @param gravity   显示位置
     */
    public void showToastCustom(Context context,String msg,int gravity){
        cancle();
        if (TextUtils.isEmpty(msg)){
            return;
        }
        View layout = View.inflate(context,R.layout.toast_msg,null);//获取布局文件
        TextView txtMsg = layout.findViewById(R.id.toast_txt);  //获取需要显示的文字
        txtMsg.setText(msg);//设置文字

        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);  //显示时长
        //判断需要显示的位置
        //setGravity(参数1:显示的位置, 参数2:X方向偏移, 参数3:Y方向偏移);
        if (gravity == Gravity.TOP){
            toast.setGravity(gravity,0,MARGIN_DP);
        }else if (gravity == Gravity.BOTTOM){
            toast.setGravity(gravity,0,MARGIN_DP);
        }else {
            toast.setGravity(gravity,0,0);
        }
        toast.setView(layout);
        toast.show();   //记得要把Toast显示出来
    }

5、使用

   CustomToast.getInstance().showToastCustom(this,"显示顶部", Gravity.TOP);
   CustomToast.getInstance().showToastCustom(this,"显示底部", Gravity.BOTTOM);
   CustomToast.getInstance().showToastCustom(this,"显示中间", Gravity.CENTER);

7、效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
8、CustomToast完成代码

public class CustomToast {
    private static CustomToast instance;
    private Toast toast;
    private final int MARGIN_DP = 200;

    public CustomToast() {
    }
    public static CustomToast getInstance(){
        if (instance == null){
            synchronized (CustomToast.class){
                if (instance == null){
                    instance = new CustomToast();
                }
            }
        }
        return instance;
    }
    public void cancle(){
        if (toast != null){
            toast.cancel();
            toast = null;
        }
    }

    /**
     * 显示自定义布局
     * @param context 上下文对象
     * @param msg   显示内容
     * @param gravity   显示位置
     */
    public void showToastCustom(Context context,String msg,int gravity){
        cancle();
        if (TextUtils.isEmpty(msg)){
            return;
        }
        View layout = View.inflate(context,R.layout.toast_msg,null);//获取布局文件
        TextView txtMsg = layout.findViewById(R.id.toast_txt);  //获取需要显示的文字
        txtMsg.setText(msg);//设置文字

        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);  //显示时长
        //判断需要显示的位置
        //setGravity(参数1:显示的位置, 参数2:X方向偏移, 参数3:Y方向偏移);
        if (gravity == Gravity.TOP){
            toast.setGravity(gravity,0,MARGIN_DP);
        }else if (gravity == Gravity.BOTTOM){
            toast.setGravity(gravity,0,MARGIN_DP);
        }else {
            toast.setGravity(gravity,0,0);
        }
        toast.setView(layout);
        toast.show();   //记得要把Toast显示出来
    }
}

撒花~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值