要时刻提醒自己,这是怎么实现的?他的原理是什么?我该什么设计?我也能举一反三么?
终究一句话就是:看源码!看源码!看源码!
源码中:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) { Toast result = new Toast(context); LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); tv.setText(text); result.mNextView = v; result.mDuration = duration; return result; }那我们也可以自定义布局,再通过view加载。
这是我的Toast工具类:
public class ToastUtils { private Context mContext; private static ToastUtils toastUtils = null; private Toast toast; private LayoutInflater inflate; public static final int LENGTH_LONG = 1; public static final int LENGTH_SHORT = 0; private ToastUtils (Context context) { if (context == null || context.getApplicationContext() == null) { throw new NullPointerException("context not be null"); } this.mContext = context.getApplicationContext(); /** * 若只要存在一个Toast实例,那写在这个位置,因为单例只会创建一个ToastUtils实例。在Activity * 中同时调用不同风格的show方法,只会显示最后一个;若想同时显示两个Toast风格的通知,那么创建 * 新的Toast实例应该写在自己的show方法中。同时调用时,就会依次显示(消息显示会放在队列中)。 * */ toast = new Toast(mContext); inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public static ToastUtils getInstance (Context context) { if (toastUtils == null) { toastUtils = new ToastUtils(context); } return toastUtils; } /** * 默认Toast风格 * */ public ToastUtils showDefeaultA(String content, int duration) { toast = Toast.makeText(mContext, content, duration); return this; } /** * 底部居中设置:文字背景图 * */ public ToastUtils showBottonCenterB(int bg, String content, int duration){ View view = inflate.inflate(R.layout.view_toast_1, null); TextView tvContent = (TextView) view.findViewById(R.id.toast_content_1); tvContent.setText(content); tvContent.setBackgroundResource(bg); toast.setView(view); toast.setDuration(duration); return this; } /** * 中间居中设置:文字背景图 * */ public ToastUtils showInCenterB(int bg, String content, int duration) { View view = inflate.inflate(R.layout.view_toast_1, null); toast.setGravity(Gravity.NO_GRAVITY, 0, 0); TextView tvContent = (TextView) view.findViewById(R.id.toast_content_1); tvContent.setText(content); tvContent.setBackgroundResource(bg); toast.setView(view); toast.setDuration(duration); return this; } /** * 设置任意位置:文字图片背景 * */ public ToastUtils showSetLocationB(int bg, String content,int gravity, int x, int y, int duration) { View view = inflate.inflate(R.layout.view_toast_1, null); TextView tvContent = (TextView) view.findViewById(R.id.toast_content_1); tvContent.setText(content); tvContent.setBackgroundResource(bg); toast.setGravity(gravity, x, y); toast.setView(view); toast.setDuration(duration); return this; } /** * 底部居中位置:图片+文字 * */ public ToastUtils showBottonCenterC(int icon, String content, int duration) { View view = inflate.inflate(R.layout.view_toast_2, null); ImageView toastIcon = (ImageView) view.findViewById(R.id.toast_icon_2); TextView toastContent = (TextView) view.findViewById(R.id.toast_content_2); toastIcon.setBackgroundResource(icon); toastContent.setText(content); toast.setView(view); toast.setDuration(duration); return this; } /** * 中间居中位置:图片+文字 * */ public ToastUtils showInCenterC(int icon, String content, int duration) { View view = inflate.inflate(R.layout.view_toast_2, null); ImageView toastIcon = (ImageView) view.findViewById(R.id.toast_icon_2); TextView toastContent = (TextView) view.findViewById(R.id.toast_content_2); toastIcon.setBackgroundResource(icon); toastContent.setText(content); toast.setGravity(Gravity.NO_GRAVITY, 0, 0);//显示的位置 toast.setView(view); toast.setDuration(duration); return this; } /** * 设置任意位置:图片+文字 * */ public ToastUtils showSetLocationC(int icon, String content,int gravity, int x, int y, int duration) { View view = inflate.inflate(R.layout.view_toast_2, null); toast.setGravity(gravity, x, y); ImageView toastIcon = (ImageView) view.findViewById(R.id.toast_icon_2); TextView toastContent = (TextView) view.findViewById(R.id.toast_content_2); toastIcon.setBackgroundResource(icon); toastContent.setText(content); toast.setView(view); toast.setDuration(duration); return this; } public ToastUtils show() { toast.show(); return this; } }我写文章的主要目的有两个,一个是方便自己+提升自己,一个是让初学者一起共同进步
当然这个学习过程我也是阅读很多人的文章,我本身不NB,我希望自己变得NB