自定义toast
来源:互联网 发布:免费手机阅读软件 编辑:程序博客网 时间:2024/06/01 10:24
public class GCSimpleToast { private static LayoutInflater mInflater; private static Toast mToast; private static View mView; public static void ok(Context context, String msg) { mInflater = LayoutInflater.from(context); mView = mInflater.inflate(R.layout.toast_ok, null); initSetTextViewMsg(msg); mToast = new Toast(context); mToast.setView(mView); mToast.setDuration(Toast.LENGTH_SHORT); mToast.show(); }
1,准备好你将需要的布局
private static TextView initSetTextViewMsg(String msg) { TextView mTextView = (TextView) mView.findViewById(R.id.tv_toast); mTextView.setText(msg); return mTextView; }
2,toast信息
public enum BGToast { OK,//蓝色 ERROR,//红色 INFO,//绿色 MUTED,//灰色 WARNING//橙色 } public static void configBG(BGToast bgToast, String message) { switch (bgToast) { case OK: GCSimpleToast.ok(GCApp.getInstance(), message); break; case ERROR: GCSimpleToast.error(GCApp.getInstance(), message); break; case INFO: GCSimpleToast.info(GCApp.getInstance(), message); break; case MUTED: GCSimpleToast.muted(GCApp.getInstance(), message); break; case WARNING: GCSimpleToast.warning(GCApp.getInstance(), message); break; } }
3,枚举,选择需要的样式
public void showMessage(final String message) { runOnUiThread(new Runnable() { @Override public void run() { GCSimpleToast.info(GCBaseFragmentActivity.this, message); } }); }
public void showMessage(final String message, final GCSimpleToast.BGToast type) { runOnUiThread(new Runnable() { @Override public void run() { GCSimpleToast.configBG(type, message); } }); }
4,在基类里封装一下
showMessage(getResources().getString(R.string.toast_new_version), GCSimpleToast.BGToast.INFO);
showMessage(getResources().getString(R.string.toast_new_version));
5,使用。
1 0
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- Toast自定义
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- linux基础 --- grep
- Matlab 三维图像绘制(1)——纹理贴图、球体
- JavaScript中的回调函数
- ul 居中方法(兼容 IE)
- #ifdef __cplusplus代码段
- 自定义toast
- 截取HTML中代码块生成图片
- 图片翻页
- 自定义按钮(文字和图标)
- c++l类中static成员函数和成员数据变量使用
- 鸟哥的Linux私房菜(基础篇)-第零章、计算机概论(零.5)
- 滴滴柳青、58同城、美团创始人引领《财富》商界精英
- Defer, Panic, and Recover
- POP到指定的界面