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

时间:2021-12-18 00:17:51

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

 package cc.wulian.smarthomev6.support.utils;

 import android.support.annotation.StringRes;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast; import cc.wulian.smarthomev6.R;
import cc.wulian.smarthomev6.main.application.MainApplication; /**
* Created by huxc on 2017/6/15.
* 统一弹Toast
*/ public class ToastUtil {
private static Toast toast; public static void show(String text) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
          //这个地方第二个参数需要为null
toast.setText(text);
} else {
toast.setText(text);
}
toast.show();
} public static void show(@StringRes int resId) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(resId);
} else {
toast.setText(resId);
}
toast.show();
} /**
* 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上
*/
public static void single(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
} public static void singleLong(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_LONG);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
} /**
* 多行居中显示
*/
public static void singleCenter(@StringRes int msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
toast.show();
} /**
* 多行居中显示
*/
public static void singleCenter(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
toast.show();
} /**
* 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上
*/
public static void single(@StringRes int msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
}
}

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