Android工具类Toast自定义图片和文字
作者:zhouzhangfu 发布时间:2021-11-15 08:22:44
标签:Android,Toast
有时候我们做Android开发,需要弹一个用户提示,但是有时候设计的提示弹窗是带有图片的,我们每次写一个特别麻烦。所以我特地封装了一个工具类,在需要弹窗的地方调用对应的方法即可,根据需要可以传文字和图片资源id,方便自定义Toast弹窗提示。
下面是效果图
自定义工具类代码
/**
* Created by zzf on 2018/7/7.
* 一个自定义的吐司工具类,可以修改任意布局
*/
public class ToastUtils {
private static Context mContext = OcreanSonicApplication.getContext();
public static void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
/**
* 带图片的吐司提示
* @param text
*/
public static void showCustomImgToast(String text) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 带图片的吐司提示
* 通过参数传递,可是设置吐司的图片和文字内容
* @param text
*/
public static void showCustomImgToast(String text,int imgResId) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 不带图片的吐司提示
* @param text
*/
public static void showCustomToast(String text) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setVisibility(View.GONE);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 带图片的吐司,设置吐司弹出的位置为屏幕中心
* @param text
*/
public static void showCustomToastCenter(String text) {
showCustomToastCenter(text, R.mipmap.pd_ic_finish);
}
/**
* 带图片的吐司,设置吐司弹出的位置为屏幕中心
* 通过参数传递,可是设置吐司的图片和文字内容
* @param text
*/
public static void showCustomToastCenter(String text, int imgResId) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(imgResId);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
在自定义Toast中引用xml布局,用来放置图片和文字,设置id,可以任意在Java代码中设置
<?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="vertical">
<!-- android:minHeight="80dp"-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/shape_toast"
android:minWidth="120dp"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<!--android:background="@drawable/toast_bg"-->
<ImageView
android:id="@+id/toast_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:background="@mipmap/pd_ic_finish"/>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_gravity="center"
android:text="保存成功"
android:textColor="#ffffff"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
来源:https://blog.csdn.net/zhouzhangfu/article/details/82114334


猜你喜欢
- 一、短信登录验证机制原理分析了解短信验证码的登陆机制之前,我们首先是要了解用户账号密码登陆的机制是如何的,我们来简要分析一下Spring S
- 使用resilience4j的库和Spring Boot设计高弹性的微服务。微服务本质上是分布式的。当您使用分布式系统时,请始终记住这一第一
- 案例需求:访问带有验证码的登录页面login.jsp用户输入用户名,密码以及验证码。如果用户名和密码输入有误,跳转登录页面,提示:用户名或密
- 场景Android中点击按钮启动另一个Activity以及Activity之间传值:https://www.jb51.net/article
- 前言在看一本关于高性能编程的时候发现 Java8 中关于接口的新特性的介绍,这个特性是真的棒,解决了一个接口中有多个方法,但并不想实现该接口
- C#时间/日期格式大全,C#时间/日期函数大全有时候我们要对时间进行转换,达到不同的显示效果默认格式为:2005-6-6 14:33:34如
- Swing 程序用JFrame 对象实现了它们的窗口。JFrame 类是AWT Frame 类的一个子类。它还加入了一些Swing 所独有的
- 实例如下:Enumeration rnames=request.getParameterNames();for (Enumeration e
- 1.定义字符串字符串常见的构造方式如下:String s1 = "with";String s2 = new Strin
- 就网络和应用程序而言,键盘快捷键很重要,今天我们要谈的便是让这类快捷键得以在Flutter运作的小部件:Focus、Shortcuts和Ac
- 在Java应用程序的部署和调优过程中,合理配置JVM参数是提升性能和稳定性的关键之一。本文将介绍一些常用的JVM参数,并给出具体的使用例子和
- 照片墙这种功能现在应该算是挺常见了,在很多应用中你都可以经常看到照片墙的身影。它的设计思路其实也非常简单,用一个GridView控件当作“墙
- 本文实例为大家分享了Android实现密码明密文切换的具体代码,供大家参考,具体内容如下小眼睛在密码栏右边!奉上我使用的素材:添加图片到re
- 本文实例为大家分享了Android实现拍照或者选取本地图片的具体代码,供大家参考,具体内容如下总体流程从selectPhotoActivit
- 前言延迟初始化 是一种将对象的创建延迟到第一次需要用时的技术,换句话说,对象的初始化是发生在真正需要的时候才执行,值得注意的是,术语&nbs
- 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示生产者向空间里存放数据,而消费者取用数据
- Android刚兴起的时候,着实让一些小众软件火了一把,切水果,Tom猫,吹裙子就是其中的代表,当然还有实用性很强的关机重启软件,我们去百度
- JavaFx中其实也可以直接使用字体图标iconfont的,只需要加载ttf字体文件,之后设置unicode即可,具体可以看我给出的代码既然
- 为什么要使用克隆? 想对一个对象进行处理,又想保留原有的数据进行接下来的操作,就需要克隆了,Java语言中克隆针对的是类的实例。如何实现对象
- 一、token与cookie相比较的优势1、支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的;2、无状态化,服务端无