Android工具類Toast自定義圖片和文字
有時(shí)候我們做Android開發(fā),需要彈一個(gè)用戶提示,但是有時(shí)候設(shè)計(jì)的提示彈窗是帶有圖片的,我們每次寫一個(gè)特別麻煩。所以我特地封裝了一個(gè)工具類,在需要彈窗的地方調(diào)用對(duì)應(yīng)的方法即可,根據(jù)需要可以傳文字和圖片資源id,方便自定義Toast彈窗提示。
下面是效果圖

自定義工具類代碼
/**
* Created by zzf on 2018/7/7.
* 一個(gè)自定義的吐司工具類,可以修改任意布局
*/
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();
}
/**
* 帶圖片的吐司提示
* 通過參數(shù)傳遞,可是設(shè)置吐司的圖片和文字內(nèi)容
* @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();
}
/**
* 帶圖片的吐司,設(shè)置吐司彈出的位置為屏幕中心
* @param text
*/
public static void showCustomToastCenter(String text) {
showCustomToastCenter(text, R.mipmap.pd_ic_finish);
}
/**
* 帶圖片的吐司,設(shè)置吐司彈出的位置為屏幕中心
* 通過參數(shù)傳遞,可是設(shè)置吐司的圖片和文字內(nèi)容
* @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布局,用來放置圖片和文字,設(shè)置id,可以任意在Java代碼中設(shè)置
<?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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Activity與Service之間交互并播放歌曲的實(shí)現(xiàn)代碼
以下是對(duì)Activity與Service之間交互并播放歌曲的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
Android實(shí)現(xiàn)打開各種文件的intent方法小結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)打開各種文件的intent方法,結(jié)合實(shí)例形式總結(jié)分析了Android針對(duì)HTML、圖片文件、pdf文件、文本文件、音頻文件、視頻文件等的intent打開方法,需要的朋友可以參考下2016-08-08
Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖片的切換顯示、透明度、大小調(diào)整等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
詳解Android JetPack之LiveData的工作原理
這篇文章主要介紹了詳解Android JetPack之LiveData的工作原理,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03
Android開發(fā)中g(shù)radle下載緩慢的問題級(jí)解決方法
本文介紹了解決Android開發(fā)中Gradle下載緩慢問題的幾種方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-02-02
android 自定義圓角button效果的實(shí)例代碼(自定義view Demo)
這篇文章主要介紹了android 自定義圓角button(自定義View Demo),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
android 仿微信demo——微信主界面實(shí)現(xiàn)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06
Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架功能
這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

