android自定义Toast样式和显示方式

问题: 1.android 开发中如果不停的触发显示Toast,会造成Toast一个接一个的弹出,非常影响用户体验。 2.android设备有千万个,每个设备的Toast的背景有可能不一样,造成在应用内显示时造成显示不够清晰

在这里我封装了一个Toast工具类可以实现弹出下一个Toast时检查当前是否有显示Toast,有的话重用当前Toast,避免重复弹出。 可以自定义Toast显示的view,实现你想要的样式,实现领导的梦想!!!

Toast工具类

kotlin 复制代码
/**
 * Created by Aaron on 2019/07/18.
 *
 * 弹出toast提示工具类
 */
object ToastUtils {

    private var mToast: Toast? = null  //toast样式
    private val mMsg: String? = null  //上一次弹出的内容
    private var mToastView: ToastView? = null  //自定义view
    private var mToastGravity:Int = -1  //位置

    /**
     * 弹出提示
     * @param msg  提示信息
     * @param time  显示时间
     */
    fun showToast(msg: String?, time: Int, context: Context?) {
        if (mToast == null || mMsg != null && msg != mMsg) {
            mToast = Toast.makeText(context, msg, time)
            if (mToastView != null) {
                mToast!!.view = mToastView
                mToastView!!.setText(msg!!)
            } else {
                mToast!!.setText(msg)
            }
        } else {
            if (mToastView != null && mToast!!.view != mToastView) {
                mToast!!.view = mToastView
            }
            if (mToastView != null) {
                mToastView!!.setText(msg!!)
            } else {
                mToast!!.setText(msg)
            }
            mToast!!.duration = time
        }
        if (mToastGravity != -1) {
            mToast!!.setGravity(mToastGravity, 0, 0)
        }

        //不设置的话,最高显示到状态栏下面
        mToast!!.view.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        mToast!!.show()
    }

    /**
     * 弹出提示信息
     * @param msgId  提示信息id
     * @param time  显示时间
     */
    fun showToast(msgId: Int, time: Int, context: Context?) {
        showToast(context?.getString(msgId), time, context)
    }

    /**
     * 弹出短时间提示
     * @param msg  提示信息
     */
    fun showShortToast(msg: String, context: Context?) {
        showToast(msg, Toast.LENGTH_SHORT, context)
    }

    fun showShortToast(msgId:Int, context: Context?) {
        showToast(msgId, Toast.LENGTH_SHORT, context)
    }

    /**
     * 弹出长时间提示
     * @param msg  提示信息
     */
    fun showLongToast(msg: String, context: Context?) {
        showToast(msg, Toast.LENGTH_LONG, context)
    }

    /**
     * 关闭当前Toast
     */
    fun cancelCurrentToast() {
        if (mToast != null) {
            mToast!!.cancel()
        }
    }

    fun reToast(msg: String) {
        Toast.makeText(JblBaseApplication.sInstance, msg, Toast.LENGTH_SHORT).show()
    }

    fun reToast(msgId: Int) {
        Toast.makeText(JblBaseApplication.sInstance, msgId, Toast.LENGTH_SHORT).show()
    }

    fun setToastView(context: Context?) {
        mToastView = ToastView(context!!)
    }

    fun setToastGravity(gravity: Int) {
        mToastGravity = gravity
    }

    /**
     * 重置toast 信息
     */
    fun resetToast() {
        mToastView = null
        mToastGravity = -1
        mToast = null
    }
}

自定义view

kotlin 复制代码
/**
 * Created by Aaron on 2019-07-18.
 *
 * 自定义toast view
 */
class ToastView : FrameLayout {

    private var toastText: TextView? = null

    constructor(context: Context) : super(context) {

        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {

        init(context)
    }

    private fun init(context: Context) {
        addView(View.inflate(context, R.layout.toast_view, null))
        toastText = findViewById(R.id.toastText)
    }

    fun setText(text: String) {
        toastText!!.text = text
    }

    fun setTextSize(size: Int) {
        toastText!!.textSize = size.toFloat()
    }

    fun setTextColor(color: Int) {
        toastText!!.setTextColor(color)
    }
}

自定义view layout

ini 复制代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#88000000">

    <TextView
            android:id="@+id/toastText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/text_color_white"
            android:textSize="30sp"
            android:paddingLeft="60dp"
            android:paddingTop="10dp"
            android:paddingRight="60dp"
            android:paddingBottom="10dp"
            tools:ignore="MissingConstraints"/>
</android.support.constraint.ConstraintLayout>
相关推荐
SharpCJ21 分钟前
Android 开发者为什么必须掌握 AI 能力?端侧视角下的技术变革
android·ai·aigc
_李小白1 小时前
【OSG学习笔记】Day 38: TextureVisitor(纹理访问器)
android·笔记·学习
JJay.1 小时前
Kotlin 高阶函数学习指南
android·开发语言·kotlin
jinanwuhuaguo1 小时前
截止到4月8日,OpenClaw 2026年4月更新深度解读剖析:从“能力回归”到“信任内建”的范式跃迁
android·开发语言·人工智能·深度学习·kotlin
JJay.2 小时前
Android Kotlin 协程使用指南
android·开发语言·kotlin
BLUcoding3 小时前
Android 布局介绍
android
summerkissyou19873 小时前
android-蓝牙-状态和协议值总结及监听例子
android·蓝牙
徒 花3 小时前
数据库知识复习05
android·数据库
提子拌饭1335 小时前
番茄时间管理:鸿蒙Flutter 实现的高效时间管理工具
android·flutter·华为·架构·开源·harmonyos·鸿蒙
4311媒体网5 小时前
帝国CMS二次开发实战:精准实现“最新资讯”标识与高亮判断
android