Android(安卓)Toast 部分手机无法显示问题(如小米手机)解决
16lz
2021-01-26
Android项目中使用了的Toast,测试过程中发现有部分手机无法正常显示Toast,尝试了数种解决方案后,问题得以解决,以下为封装的工具类,统一的Toast框,也能避免反复弹出不同的Toast框。
public class LKToastUtil { private static Toast mToast; @SuppressLint("StaticFieldLeak") private static TextView tv_toastMsg; private static float bottomHeight = 60; /** * 快速连续弹Toast * * @param msg 需要显示的内容 */ public static void showToastLong(final String msg) { if (msg.equals("")) { return; } LKBaseApplication.getHandler().post(new Runnable() { @Override public void run() { showToast(msg, Toast.LENGTH_LONG); } }); } /** * 快速连续弹Toast * * @param msg 需要显示的内容 */ public static void showToastShort(final String msg) { if (msg.equals("")) { return; } LKBaseApplication.getHandler().post(new Runnable() { @Override public void run() { showToast(msg, Toast.LENGTH_SHORT); } }); } /** * 显示Toast * * @param msg 需要显示的内容 * @param duration 显示时长 */ private static void showToast(final String msg, final int duration) { if (mToast == null) { mToast = new Toast(LKBaseApplication.getApplication()); @SuppressLint("InflateParams") View view = LayoutInflater.from(LKBaseApplication.getApplication()).inflate(R.layout.view_toast, null); tv_toastMsg = view.findViewById(R.id.tv_toastMsg); mToast.setView(view); } tv_toastMsg.setText(msg); mToast.setDuration(duration); mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, LKScreenUtil.dp2px(bottomHeight)); mToast.show(); }}
其中LKBaseApplication.getHandler()为Application中定义的全局Handler,如果没有定义的也可以自行传入即可。
更多相关文章
- Android Studio 快捷键
- 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优
- Android(安卓)- 用WebView开发简单的浏览器
- 自定义ThreadPoolExecutor
- Android(安卓)高德地图,显示地图,并定位当前的位置
- Android自定义视图控件
- Android(安卓)webView 中loadData方法加载 带中文时出现乱码
- 如何定制PopupWindow弹出框及方法介绍
- Android(安卓)RoboGuice 使用指南(8):Provider Bindings