小米手机Toast显示带应用名称问题解决方法

近期为了适配刘海屏,向公司申购了一步小米8的手机,然后测试人员那边测出来一堆适配的问题,其中有一个每一个Toast会显示app的名称+显示的内容,然后网上查找了一下解决方法记录一下,顺便封装了ToastUtil方便调用。

  1 package cc.wulian.smarthomev6.support.utils;
  2 
  3 import android.support.annotation.StringRes;
  4 import android.view.Gravity;
  5 import android.widget.TextView;
  6 import android.widget.Toast;
  7 
  8 import cc.wulian.smarthomev6.R;
  9 import cc.wulian.smarthomev6.main.application.MainApplication;
 10 
 11 /**
 12  * Created by huxc on 2017/6/15.
 13  * 统一弹Toast
 14  */
 15 
 16 public class ToastUtil {
 17     private static Toast toast;
 18 
 19     public static void show(String text) {
 20         if (toast == null) {
 21             toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
          //这个地方第二个参数需要为null
22 toast.setText(text); 23 } else { 24 toast.setText(text); 25 } 26 toast.show(); 27 } 28 29 public static void show(@StringRes int resId) { 30 if (toast == null) { 31 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); 32 toast.setText(resId); 33 } else { 34 toast.setText(resId); 35 } 36 toast.show(); 37 } 38 39 /** 40 * 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上 41 */ 42 public static void single(String msg) { 43 if (toast == null) { 44 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); 45 toast.setText(msg); 46 } else { 47 toast.setText(msg); 48 } 49 toast.show(); 50 } 51 52 public static void singleLong(String msg) { 53 if (toast == null) { 54 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_LONG); 55 toast.setText(msg); 56 } else { 57 toast.setText(msg); 58 } 59 toast.show(); 60 } 61 62 /** 63 * 多行居中显示 64 */ 65 public static void singleCenter(@StringRes int msg) { 66 if (toast == null) { 67 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); 68 toast.setText(msg); 69 } else { 70 toast.setText(msg); 71 } 72 ((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER); 73 toast.show(); 74 } 75 76 /** 77 * 多行居中显示 78 */ 79 public static void singleCenter(String msg) { 80 if (toast == null) { 81 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); 82 toast.setText(msg); 83 } else { 84 toast.setText(msg); 85 } 86 ((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER); 87 toast.show(); 88 } 89 90 /** 91 * 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上 92 */ 93 public static void single(@StringRes int msg) { 94 if (toast == null) { 95 toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); 96 toast.setText(msg); 97 } else { 98 toast.setText(msg); 99 } 100 toast.show(); 101 } 102 }

Toast.makeText()方法的第二个参数传null,然后mtoast.settext(text)重新设置一下

posted @ 2018-11-20 10:13  养只柯基萌萌哒  阅读(2992)  评论(0编辑  收藏  举报