🐳 《Android》 安卓开发教程 - 自定义 Toast

一、什么是Toast

在 Android 开发中,Toast 是一种常用的轻量级提示方式,适用于向用户展示短暂的信息。但随着用户体验要求的提升,原生 Toast 已无法满足项目需求。本文将通过一个完整的自定义 Toast 工具类实现,讲解原生 Toast 的问题、自定义的优势,以及如何实现美观易用的提示弹窗。

Toast 是 Android 提供的一种用于提示用户信息的方式。它通常位于屏幕底部,自动消失,无需用户交互。使用简单,一行代码即可:

java 复制代码
Toast.makeText(context, "这是原生Toast", Toast.LENGTH_SHORT).show();

二、原生 Toast 的常见弊端

尽管原生 Toast 使用方便,但在实际项目中存在不少缺陷:

问题 描述
样式单一 只能显示文字,不能自定义图标、背景、布局
不可控性差 无法灵活控制位置、时长、动画等效果
易与系统提示混淆 无品牌感,用户难以区分来自 App 的提示与系统提示
多 Toast 堆叠问题 连续弹出多个 Toast 时会出现覆盖、排队等问题

因此,自定义 Toast 成为了更专业 App 开发的标配。

三、自定义 Toast 的优势

相比原生方案,自定义 Toast 提供了更高的灵活性和可控性:

  • ✅ 支持自定义布局(图标 + 文本 + 背景)
  • ✅ 支持不同场景提示(信息、成功、警告、错误等)
  • ✅ 可自由控制位置偏移量延时显示
  • ✅ 更符合 App 的 UI 风格与交互逻辑

四、项目实战:自定义 Toast 工具类封装

本项目使用了 hjq-toast 框架实现更高级的 Toast 控制,并通过 ToastUtils 工具类进行统一封装,便于调用与维护。

1. 项目级 build.gradle(一般不需要修改,但建议保持最新)

确保项目已经启用了 mavenCentral

xml 复制代码
allprojects {
    repositories {
        google()
        mavenCentral()  // <--- 必须要有
    }
}
2. 模块级 build.gradle 添加依赖:
xml 复制代码
dependencies {
    implementation 'com.github.getActivity:Toaster:12.8'
}
3. 自定义工具类(ToastUtils)
java 复制代码
public class ToastUtils {

    public static void showInfo(String s) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_info, Gravity.BOTTOM,0, 50);
        Toaster.show(params);
    }

    public static void showSuccess(String s) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_success, Gravity.BOTTOM,0, 50);
        Toaster.show(params);
    }

    public static void showSuccessDelay(String s, long time) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_success, Gravity.BOTTOM,0, 50);
        Toaster.delayedShow(params, time);
    }

    public static void showWarning(String s) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_warn, Gravity.BOTTOM,0, 50);
        Toaster.show(params);
    }

    public static void showWarningDelay(String s, long time) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_warn, Gravity.BOTTOM,0, 50);
        Toaster.delayedShow(params, time);
    }

    public static void showError(String s) {
        ToastParams params = new ToastParams();
        params.text = s;
        params.style = new CustomToastStyle(R.layout.toast_error, Gravity.BOTTOM, 0, 50);
        Toaster.show(params);
    }

    public static void showInvalidUserBean() {
        ToastParams params = new ToastParams();
        params.text = "获取的用户信息无效,请重试";
        params.style = new CustomToastStyle(R.layout.toast_error, Gravity.BOTTOM, 0, 50);
        Toaster.show(params);
    }
}
4. 自定义布局示例

(toast_info.xml)

xml 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_hint_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingStart="20dp"
    android:paddingTop="12dp"
    android:paddingEnd="20dp"
    android:paddingBottom="12dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/toast_info_ic" />

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:lineSpacingExtra="5dp" />
</LinearLayout>

(toast_error.xml)

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/toast_error_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingStart="20dp"
    android:paddingTop="12dp"
    android:paddingEnd="20dp"
    android:paddingBottom="12dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/toast_error_ic" />

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="5dp"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        tools:text="我是 Toast 文本" />

</LinearLayout>

(toast_success.xml)

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/toast_success_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingStart="20dp"
    android:paddingTop="12dp"
    android:paddingEnd="20dp"
    android:paddingBottom="12dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/toast_success_ic" />

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="5dp"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        tools:text="我是 Toast 文本" />

</LinearLayout>

五、调用方式

在任意需要提示信息的地方,调用如下方法即可:

java 复制代码
ToastUtils.showSuccess("成功的 Toast");

ToastUtils.showError("失败的 Toast");

六、结语

通过对原生 Toast 的封装与美化,不仅提升了 App 的 UI 品质,也增强了用户的使用体验。自定义 Toast 已成为现代 Android 应用不可或缺的一部分。推荐结合 UI 设计师的方案,打造符合品牌调性的统一提示组件。

如需进一步优化,还可以支持:

  • ✅ 动态修改字体大小、颜色
  • ✅ 多语言适配
  • ✅ 引入动画增强表现力
相关推荐
想摆烂的不会研究的研究生1 天前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
xiaolizi5674891 天前
安卓远程安卓(通过frp与adb远程)完全免费
android·远程工作
阿杰100011 天前
ADB(Android Debug Bridge)是 Android SDK 核心调试工具,通过电脑与 Android 设备(手机、平板、嵌入式设备等)建立通信,对设备进行控制、文件传输、命令等操作。
android·adb
毕设源码-郭学长1 天前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
梨落秋霜1 天前
Python入门篇【文件处理】
android·java·python
追逐时光者1 天前
精选 10 款 .NET 开源免费、功能强大的 Windows 效率软件
后端·.net
追逐时光者1 天前
一款开源、免费的 WPF 自定义控件集
后端·.net
S***q3771 天前
Spring Boot管理用户数据
java·spring boot·后端
毕设源码-郭学长1 天前
【开题答辩全过程】以 基于SpringBoot框架的民俗文化交流与交易平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
l***21781 天前
SpringBoot Maven快速上手
spring boot·后端·maven