1. 程式人生 > >小米手機Toast顯示帶應用名稱問題解決方法

小米手機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)重新設定一下