国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android工具類Toast自定義圖片和文字

2019-10-21 21:40:01
字體:
來源:轉載
供稿:網友

有時候我們做Android開發,需要彈一個用戶提示,但是有時候設計的提示彈窗是帶有圖片的,我們每次寫一個特別麻煩。所以我特地封裝了一個工具類,在需要彈窗的地方調用對應的方法即可,根據需要可以傳文字和圖片資源id,方便自定義Toast彈窗提示。

下面是效果圖

Android,工具類,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>

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会昌县| 治县。| 手机| 华亭县| 孟州市| 浦东新区| 句容市| 来安县| 米泉市| 安远县| 巴塘县| 密山市| 阜南县| 新河县| 本溪市| 宁安市| 同德县| 钟山县| 浮梁县| 东港市| 绥江县| 禹州市| 长寿区| 乌鲁木齐市| 阳曲县| 聂拉木县| 清远市| 西城区| 克山县| 修水县| 龙江县| 勃利县| 吴桥县| 新巴尔虎左旗| 库车县| 水富县| 井陉县| 曲沃县| 中牟县| 五华县| 怀化市|