Browse Source

修改toast显示(部分pad显示过快出现不显示的问题)

Pq 2 years ago
parent
commit
5f75985abe
1 changed files with 26 additions and 7 deletions
  1. 26 7
      BaseLibrary/src/main/java/com/cooleshow/base/utils/ToastUtil.java

+ 26 - 7
BaseLibrary/src/main/java/com/cooleshow/base/utils/ToastUtil.java

@@ -16,12 +16,17 @@ import com.cooleshow.base.R;
 
 public class ToastUtil {
 
-    public static ToastUtil instance;
+    public volatile static ToastUtil instance;
     private Toast toast;
+    private TextView mContentView;
 
     public static ToastUtil getInstance() {
         if (null == instance) {
-            instance = new ToastUtil();
+            synchronized (ToastUtil.class) {
+                if (null == instance) {
+                    instance = new ToastUtil();
+                }
+            }
         }
         return instance;
     }
@@ -42,13 +47,26 @@ public class ToastUtil {
      * 创建自定义Toasts :
      */
     public void showShort(String toastStr) {
-        if (init(toastStr, Toast.LENGTH_SHORT) == null) {
-            toast = new Toast(Utils.getApp());
-            toast.setText(toastStr);
-            toast.setDuration(Toast.LENGTH_SHORT);
-            toast.show();
+        if (mContentView == null) {
+            int dp26 = SizeUtils.dp2px(26);
+            int dp10 = SizeUtils.dp2px(10);
+            mContentView = new TextView(Utils.getApp());
+            mContentView.setGravity(Gravity.CENTER);
+            mContentView.setBackgroundResource(R.drawable.shape_toast);
+            mContentView.setTextSize(15);
+            mContentView.setTextColor(Utils.getApp().getResources().getColor(R.color.white));
+            mContentView.setPadding(dp26, dp10, dp26, dp10);
         }
+        mContentView.setText(toastStr);
+        ToastUtils.getDefaultMaker().setGravity(Gravity.CENTER,0,0).show(mContentView);
+//        if (init(toastStr, Toast.LENGTH_SHORT) == null) {
+//            toast = new Toast(Utils.getApp());
+//            toast.setText(toastStr);
+//            toast.setDuration(Toast.LENGTH_SHORT);
+//            toast.show();
+//        }
     }
+
     private Toast init(String message, int duration) {
         try {
             if (toast != null) {
@@ -66,6 +84,7 @@ public class ToastUtil {
                 contentView.setTextColor(Utils.getApp().getResources().getColor(R.color.white));
                 contentView.setPadding(dp26, dp10, dp26, dp10);
                 contentView.setText(message);
+                ToastUtils.getDefaultMaker().show(contentView);
                 toast = new Toast(Utils.getApp());
                 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                 toast.setView(contentView);