本文共 6767 字,大约阅读时间需要 22 分钟。
吐司是我们开发中常用到的,系统的吐司样式单一,位置获取麻烦.....(反正就是感觉拖泥带水的不干脆 ε=(´ο`*)))唉 ),最近上班之余搞搞MVP的基础框架,想到了写个自定义的吐司。主要为了简单,想了好久发现不会简化,那就搞个麻烦的。
1、羡慕人家Glide的链式结构,自己也搞个出来
public static ToastUtils with(Context context) { ToastUtils.context = context; if (toastUtils == null) { toastUtils = new ToastUtils(); } return toastUtils; }
这样让ToastUtils直接调用方法变成with一个,相信不会出现调用错误的情况了吧,
2、第二那必然是能显示系统的Toast,如果不能显示系统的,那就可以从入门到放弃了。更改了一下toast的布局样式,第一个是给message文字提示加上的set方法,第二个是初始化了全局的toast,防止空指针出现。然后就是基本的长短toast展示:
@SuppressLint("ShowToast") public ToastUtils shortToast() { if (toastView != null && toastView.getChildCount() > 1) { toastView = null; } else { toast.setText(message); toast.setDuration(Toast.LENGTH_SHORT); } return this; }
当然short 和long方法是一样的,只是时间设置不一样。接下来说一说自定义布局,首先用addView添加布局,经过查看了源码才知道Toast默认布局是有message这个id的,一个大写的尴尬所以跑过来改一下原来写的。
/** * 向Toast中添加自定义View */ public ToastUtils addView(View view, int position) { toastView = (LinearLayout) toast.getView(); toastView.addView(view, position); return this; }
可以给当前的View设置背景和文字颜色。然后添加布局和控件位置。
/** * 设置Toast字体 */ public ToastUtils setTextColor(int messageColor) { View view = toast.getView(); if (view != null) { TextView message = view.findViewById(R.id.message); message.setTextColor(messageColor); } return this; }
颜色字体的方法类似,就不一一叙述了。
3、最后一步当然是show方法了。自定义Toast不能忘了show 。(^U^)ノ~YO,两种写法均不影响。
/** * 显示Toast */ public void show() { toast.show(); } public ToastUtils show() { toast.show(); return this; }
4,上次忘了展示怎么使用,这次补上,
自定义布局如下:
ToastManager.CustomToast.with(context) .setMessage("来自世界各地的朋友!!") .setCustomView(R.layout.activity_login) .setCustomTime(5000) .setCustomTextColor(getResources().getColor(R.color.colorAccent)) .setGravity(Gravity.CENTER) .showToast(R.id.tv_msg) .build();
系统布局如下:
ToastManager.DefaultToast.with(context) .setMessage("来自世界各地的朋友!!!") .setBackgroundColor(Color.TRANSPARENT) .setCustomTime(5000) .setTextColor(getResources().getColor(R.color.colorAccent)) .setGravity(Gravity.BOTTOM) .build();
抽空修改了一下格式,完善了一下工具类。觉得写的不好的,你就凑合看吧!!!!
/** * 自定义Toast * * @author Lespayne */public class ToastManager { /** * 默认toast */ public static class DefaultToast extends BaseToast { private Toast toast; private String message; private int duration; private static Context context; @SuppressLint("StaticFieldLeak") private static DefaultToast normal; private LinearLayout toastView; @SuppressLint("ShowToast") public static DefaultToast with(Context context) { DefaultToast.context = context; if (normal == null) { normal = new DefaultToast(); } return normal; } @Override @SuppressLint("ShowToast") public DefaultToast setMessage(String message) { this.message = message; toast = Toast.makeText(context, message, duration); return this; } public DefaultToast setBackgroundColor(int color) { View view = toast.getView(); if (view != null) { TextView message = view.findViewById(android.R.id.message); message.setBackgroundColor(color); } return this; } public DefaultToast setTextColor(int color) { LinearLayout layout = (LinearLayout) toast.getView(); if (layout != null) { TextView view = layout.findViewById(android.R.id.message); view.setTextColor(color); } return this; } @Override public DefaultToast setCustomTime(int duration) { this.duration = duration; return this; } /** * 向系统Toast中添加自定义View */ public DefaultToast addView(View view, int position) { toastView = (LinearLayout) toast.getView(); toastView.addView(view, position); return this; } @Override public DefaultToast setGravity(int gravity) { toast.setGravity(gravity, 0, 0); return this; } @Override public void build() { if (message != null) { toast.show(); } } } /** * 自定义toast */ public static class CustomToast extends BaseToast { private Toast toast; private String message; @SuppressLint("StaticFieldLeak") private static Context context; @SuppressLint("StaticFieldLeak") private static CustomToast customToast; private TextView textView; private View layoutInflater; public static CustomToast with(Context context) { CustomToast.context = context; if (customToast == null) { customToast = new CustomToast(); } return customToast; } @SuppressLint("ShowToast") public CustomToast setCustomView(@LayoutRes int layout) { layoutInflater = LayoutInflater.from(context).inflate(layout, null); toast.setView(layoutInflater); return this; } public CustomToast showToast(@IdRes int viewId) { this.textView = layoutInflater.findViewById(viewId); textView.setText(message); return this; } @Override public CustomToast setMessage(String message) { this.message = message; toast = new Toast(context); return this; } @Override public CustomToast setCustomTime(int duration) { toast.setDuration(duration); return this; } @SuppressLint("ShowToast") public CustomToast setCustomBgColor(View view, int color) { if (view != null) { view.setBackgroundColor(color); } return this; } @SuppressLint("ShowToast") public CustomToast setCustomTextColor(int color) { if (textView != null) { textView.setTextColor(color); } return this; } @Override public CustomToast setGravity(int gravity) { toast.setGravity(gravity, 0, 0); return this; } @Override public void build() { if (toast != null) { toast.show(); } } }}
转载地址:http://qnwsi.baihongyu.com/