一、 ToastView.java

 1 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class ToastView extends LinearLayout{
private static ToastView mToastView; private TextView toastText;
private ImageView toastIcon;
private Context mContext; private View layout;
private Toast mToast; private ToastView(Context context) {
super(context,null);
mContext = context;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.toast, null); toastIcon = (ImageView) layout.findViewById(R.id.toastIcon);
toastText = (TextView) layout.findViewById(R.id.toastText);
} public static ToastView getInstance(Context context) {
if (mToastView != null) {
return mToastView;
} else {
if (context != null) {
mToastView = new ToastView(context);
return mToastView;
} else {
return null;
}
}
} public void setIconVisiblity(int visiblity){
toastIcon.setVisibility(visiblity);
} public void showToast(String str,int gravity,int xoffset,int yoffest) {
toastText.setText(str); if(mToast!=null){
mToast.setGravity(gravity,xoffset,yoffest);
mToast.show();
}else{
mToast= new Toast(mContext);
mToast.setView(layout);
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.show();
}
} public void showToast(String str,int gravity,int xoffset,int yoffest,int duration) {
toastText.setText(str);
duration = duration<=0 ? Toast.LENGTH_SHORT : duration; if(mToast!=null){
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(duration);
mToast.show();
}else{
mToast= new Toast(mContext);
mToast.setView(layout);
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(duration);
mToast.show();
}
} }

二、toast.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"> <TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="@dimen/toastHeight"
android:textColor="@color/title_text_color"
android:background="@drawable/toast_bg"
android:gravity="center"
android:paddingLeft="@dimen/toastTextPaddingLeft"
android:paddingRight="@dimen/toastTextPaddingRight"
android:textSize="@dimen/toastTextSize"/> <ImageView
android:id="@+id/toastIcon"
android:layout_width="@dimen/toastIconWidth"
android:layout_height="@dimen/toastIconWidth"
android:layout_marginLeft="@dimen/toastIconWidth"
android:scaleType="fitXY"
android:layout_centerVertical="true"
android:src="@drawable/toast_icon" />
</RelativeLayout>

三、Acitivity中调用

    //自定义的
private void showToast(String str, int offset) {
ToastView toast = ToastView.getInstance(RecommendActivity.this);
if(offset < 0){
toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,    //默认时长的Toast
(int)getResources().getDimension(R.dimen.defaultToastMarginTop));
}else{
toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,    //自定义时长的Toast
(int)getResources().getDimension(R.dimen.defaultToastMarginTop), offset);
}
}

封装的这个Toast没什么技术含量。

但是,之所以把Toast定义为单例模式,是为了防止疯狂点击按钮,出现连续不断的Toast

因为系统的Toast是维护一个队列,每次cancle只对当前的Toast 有用。所以之后的Toast还是会不停出现

这个Toast改为单例模式之后。如果当前Toast不为空,只对其进行setText。然后show(不调用show不会出现)

如果当前为空,才会新建一个ToastView。

最新文章

  1. datatables中的Options总结(1)
  2. C++ 系列:多线程资源收集
  3. HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用
  4. 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
  5. [流媒体]VLC主要模块
  6. WPF拖到、移动控件
  7. 了解 JavaScript 中的内置对象
  8. HIbernate学习笔记(六) 关系映射之多对多
  9. JavaScriptCore.framework基本用法(二)
  10. OAUTH协议简介
  11. ubuntu14.04 下安装mysql5.6
  12. (六) JavaScript 对象
  13. 【洛谷P2257】YY的GCD
  14. k8s-YAML配置文件(转)
  15. Ping 不通的原因分析
  16. Java中常用的方法
  17. Samsung_tiny4412(驱动笔记02)----ASM with C,MMU,Exception,GIC
  18. Java网络编程和NIO详解9:基于NIO的网络编程框架Netty
  19. Linux 命令学习之使用本地SecureCRT连接虚拟机上linux
  20. js只对等号左边的进行变量提升

热门文章

  1. python 矩阵转置transpose
  2. 19条ANDROID平台设计规范平台设计规范
  3. 机器学习(二十七)— EM算法
  4. 总结一下内核DEBUG中的dump_stack, BUG, BUG_ON以及panic
  5. mysql 初识数据库
  6. 2018-2019-2 20165210《网络对抗技术》Exp6 信息搜集与漏洞扫描
  7. altibase MDB的创建sequence的举例
  8. $_validate 自动验证 字段是否重复
  9. ADMEMS软件架构的4个阶段
  10. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt