Toast是安卓非常常用的消息弹框之一,但是原生的弹框过于朴素,无法适应不同场景的样式需求,因此设计一个类似于Toasty的弹框构建工具。

不废话直接上代码

import android.content.Context;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import androidx.annotation.Nullable;
import com.cpk.yeahtoast.R.color;
import com.cpk.yeahtoast.R.drawable;/**
*Yet Another Toast Util.
*@author pinkman
*/
public class YeahToast {private static final String TAG = YeahToast.class.getSimpleName();public static final int STYLE_NO = 0;public static final int STYLE_INFO = 1;public static final int STYLE_WARN = 2;public static final int STYLE_ERROR = 3;public static final int STYLE_SUCCESS = 4;private static YeahToast.StyleToast styleToast = null;public static void show(Context context, String text, @Nullable Integer length) {show(context, STYLE_NO , text, length);}public static void showInfo(Context context, String text, @Nullable Integer length) {prepare(context, STYLE_INFO , text, length).show();}public static void showError(Context context, String text, @Nullable Integer length) {prepare(context, STYLE_ERROR , text, length).show();}public static void showWarn(Context context, String text, @Nullable Integer length) {prepare(context, STYLE_WARN , text, length).show();}public static void showSuccess(Context context, String text, @Nullable Integer length) {prepare(context, STYLE_SUCCESS , text, length).show();}protected static StyleToast prepare(Context context, int style, String text, @Nullable Integer length) {if (styleToast != null) {styleToast.cancel();}
styleToast = new YeahToast.StyleToast(context, style, text, length);return styleToast;}
}

StyleToast是用于设置Toast样式的封装类,可以设为子类,也可以新建一个类。带样式的弹框是用Toast的setView方法将一个自定义的布局作为toast显示内容,本封装类设置Toast的布局有一个图标和文本。

/**
*@author pinkman
*/
public static class StyleToast {Toast toast;ViewGroup view;TextView tv;ImageView icon;public StyleToast(Context context, int style, String message, @Nullable Integer length) {this.toast = new Toast(context);this.view = new LinearLayout(context);this.view.setPadding(8, 8, 8, 8);this.tv = new TextView(context);LayoutParams imageLp = new LayoutParams(-2, -2);imageLp.setMargins(8, 8, 8, 8);this.icon = new ImageView(context);this.icon.setScaleType(ScaleType.FIT_CENTER);this.icon.setLayoutParams(imageLp);LayoutParams textLp = new LayoutParams(-2, -2);textLp.setMargins(8, 8, 8, 8);this.tv.setTextSize(16.0F);this.tv.setTextColor(context.getResources().getColor(color.colorWhite));this.tv.setText(message);this.tv.setGravity(17);this.tv.setTextAlignment(4);this.tv.setLayoutParams(textLp);this.view.addView(this.icon);this.view.addView(this.tv);this.view.setLayoutParams(new android.view.ViewGroup.LayoutParams(-2, -2));this.toast.setView(this.view);this.toast.setDuration(length == null ? 0 : length);switch(style) {case 1:this.view.setBackgroundResource(drawable.toast_style_info);this.icon.setImageResource(drawable.ic_error_outline_white_24dp);break;case 2:this.view.setBackgroundResource(drawable.toast_style_warn);this.icon.setImageResource(drawable.ic_warning_white_24dp);break;case 3:this.view.setBackgroundResource(drawable.toast_style_error);this.icon.setImageResource(drawable.ic_close_white_24dp);break;case 4:this.view.setBackgroundResource(drawable.toast_style_success);this.icon.setImageResource(drawable.ic_done_black_24dp);break;default:this.view.setBackgroundResource(drawable.toast_style_default);this.icon.setVisibility(8);}}public void show() {this.toast.show();}private void cancel() {this.toast.cancel();}}

所有资源都是xml文件,放在drawable里。

<!--边框-->
<!--toast_style_warn.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><stroke android:width="0.8dp"android:color="#555"/><solid android:color="#FA5324"/><corners android:bottomRightRadius="16dp"android:bottomLeftRadius="16dp"android:topRightRadius="16dp"android:topLeftRadius="16dp"/>
</shape><!--toast_style_success.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><stroke android:width="0.8dp"android:color="#666"/><solid android:color="#4FEE5F"/><corners android:bottomRightRadius="16dp"android:bottomLeftRadius="16dp"android:topRightRadius="16dp"android:topLeftRadius="16dp"/>
</shape><!--toast_style_error.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><stroke android:width="0.8dp"android:color="#888"/><solid android:color="#EE2344"/><corners android:bottomRightRadius="16dp"android:bottomLeftRadius="16dp"android:topRightRadius="16dp"android:topLeftRadius="16dp"/>
</shape><!--toast_style_info.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><stroke android:width="0.8dp"android:color="#555"/><solid android:color="#3366A5"/><corners android:bottomRightRadius="16dp"android:bottomLeftRadius="16dp"android:topRightRadius="16dp"android:topLeftRadius="16dp"/>
</shape><!--toast_style_default.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><stroke android:width="0.8dp"android:color="#EEE"/><solid android:color="#888"/><corners android:bottomRightRadius="16dp"android:bottomLeftRadius="16dp"android:topRightRadius="16dp"android:topLeftRadius="16dp"/>
</shape><!---------------------------------------------------------------------------------><!--ic_error_outline_white_24dp.xml-->
<vector android:height="24dp" android:tint="#FDFDFD"android:viewportHeight="24.0" android:viewportWidth="24.0"android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#FF000000" android:pathData="M11,15h2v2h-2zM11,7h2v6h-2zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector><!--ic_close_white_24dp.xml-->
<vector android:height="24dp" android:tint="#FDFDFD"android:viewportHeight="24.0" android:viewportWidth="24.0"android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#FF000000" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector><!--ic_warning_white_24dp.xml-->
<vector android:height="24dp" android:tint="#FDFDFD"android:viewportHeight="24.0" android:viewportWidth="24.0"android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#FF000000" android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z"/>
</vector><!--ic_done_white_24dp.xml-->
<vector android:height="24dp" android:tint="#FDFDFD"android:viewportHeight="24.0" android:viewportWidth="24.0"android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#FF000000" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
</vector>

使用方式很简单,比如想在Activity弹出一个长警告就用YeahToast.showWarn(this,"warn!",Toast.LENGTH_LONG);

因为Toast是系统默认的一种弹窗,无法自定义时长,只能为Toast.LENGTH_SHORT(2.5秒)或Toast.LENGTH_LONG(3秒),对自定义的支持不是很好,而且显示内容也受限制(比如不能播放声音),在Android9版本中甚至不支持播放长Toast,且在11版本中弃用了setView方法,这意味着本工具类可能就要失效了。

安卓仿Toasty消息弹框相关推荐

  1. 【原生JS】仿新浪微博名片弹框

    [原生JS]仿新浪微博名片弹框  博客已经搬家地址:http://cm2009.sinaapp.com/ 第一次用原生JS写小功能,有很多不足的地方,例如事件绑定没有使用事件委托功能,而是直接用零级D ...

  2. 系统消息发现有新的未读消息弹框提示

    //获取弹框 <script> function tips_pop(){             var MsgPop=document.getElementById("winp ...

  3. android 仿ios 底部弹出,项目需求讨论-仿ios底部弹框实现及分析

    hi,在项目开发中,有时候需要仿照ios的底部弹框做效果,比如我们在iPhone上面关闭定位的时候,就会弹出ios特有的底部弹框: 屏幕快照 2017-10-09 08.20.30 PM.png 弹框 ...

  4. android仿支付宝弹窗,AlipayPassDialog Android 仿支付宝密码键盘弹框,可以自定义样式 Dialog,调节字体颜色大小内容 @codeKK Android开源站...

    博客讲解地址,欢迎前往查看 欢迎大家 Star,老铁给鼓励呗 效果图如下: 主要功能 支持自定义文本.颜色.大小 支持自定义关闭图标 支持弹框样式 支持回调函数处理 支持数字位置随机 API 方法介绍 ...

  5. [转载]jquery 消息插件--仿QQ消息弹出提醒

    做网站时要实现一个功能,就是定时刷新由客人自己下的在线预定订单,然后提醒她们及时处理,正好jquery.messager.js可以帮我们轻松实现此功能,通过定时查询数据库记录,并通过消息插件弹出提醒她 ...

  6. wpf实现仿qq消息提示框

    1.实现步骤 1.1 另起一个窗口作为消息提示的窗口,在主窗体中调用,先处理一下消息框的展示问题, AllowsTransparency="True"  WindowStyle=& ...

  7. Vue的消息弹框换行问题

    // 之前使用如下这种弹出方式时,消息是不会换行的.this.$message.success(id);// 使用如下这种就会换行: this.$message({dangerouslyUseHTML ...

  8. 微信小程序 - 气泡菜单组件(仿微信气泡弹框显示菜单)

    前言 如果您想直接获得源代码示例,请 滑到文章最底部 克隆下载示例,超详细的注释和干净整洁的代码. 本示例能根据元素内容的宽高自动计算气泡的定位,并且气泡的内容项可以灵活的添加, 当点击一个按钮或元素 ...

  9. java 摸拟qq消息提示_java 仿qq消息提示框

    引用包: swt.jar 主类: import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import ...

最新文章

  1. linux系统目录树/内核源码目录树
  2. R密度聚类之DBSCAN模型
  3. js中匿名函数的N种写法
  4. linux 列出目录结构6,Linux系统目录结构及文件管理命令
  5. sealed关键字用法
  6. Java基础知识回顾之七 ----- 总结篇
  7. [html] table中给td设置宽度无效怎么解决?
  8. WPF 可触摸移动的ScrollViewer控件
  9. atitit 各个技术领域的top 200领域
  10. ModelState.IsValid 一直是 false的解决办法
  11. Node图片识别文字
  12. 手把手教学linux上扩容和缩减swap分区。
  13. 蓝桥杯 算法提高 ADV-143 扶老奶奶过街 逻辑推理
  14. 怒怼外媒,为中国正名,这个《流浪地球》捧红的犹太小哥太励志了
  15. python 切片器_Excel中如何使用切片器,这个太高大上了
  16. MSI (Message Signaled Interrupts)
  17. 记我的启蒙老师谢朝晖老师
  18. 小县城开什么店比较挣钱?
  19. 【软件测试】翻了下招聘APP只会点点点,很慌......测试业务?技术?
  20. Idea中诡异的错误——文件为灰色并显示一个橙色时钟图标

热门文章

  1. ios实时卡顿检测和优化方案
  2. 棋牌游戏进入游戏房间流程
  3. 基于Python猫眼票房TOP100电影数据抓取
  4. 日常随笔——m1 macbook安装和配置qt
  5. JavaScript数组属性和方法
  6. filter java exclude_Filter中排除对指定URL的过滤
  7. 树莓派系统最新系统镜像Bullseye更换镜像源
  8. OpenAI发布ChatGPT:程序员瞬间不淡定了
  9. python后端工程师岗位职责_【PYTHON后端开发工程师岗位职责_PYTHON后端开发工程师职责/工作内容】-猎聘岗位职责频道...
  10. WeUI实例(官方)