小米新版MIUI Toast会显示APP名字的问题解决

最近开发中,采用小米4来测试,由于升级了系统,突然发现,小米手机toast都会带应用名称,这点很不爽,非常影响体验,各种百度,也是看了一些大神的方法以后,自己写了一个通用的Toast类,在这里记录一下,方便以后调用。在创建Toast的时候按照上述红色的两句创建即可解决小米MIUI Toast会显示APP名字的问题。

(mToast == null) {
            //将这一句换成下面两句
//            mToast = Toast.makeText(context, showmsg, duration);
            mToast=Toast.makeText(context,null,duration);
            mToast.setText(showmsg);
        } else {
            //如果当前Toast没有消失, 直接显示内容,不需要重新设置
            mToast.setText(showmsg);
        }
 

自定义Toast工具类

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.xianglin.app.R;

public class ToastUtils {

    private static Toast mToast;

    private ToastUtils() {
        throw new AssertionError();
    }

    public static void show(Context context, int resId) {
        show(context, context.getResources().getText(resId), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration) {
        show(context, context.getResources().getText(resId), duration);
    }

    public static void show(Context context, CharSequence text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, CharSequence text, int duration) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        if (context == null) {
            return;
        }
        if (mToast != null) {
            mToast.setText(text);
        } else {
            //如果这个Context是Activity,而Toast是异步弹出,有可能弹出时Activity已经结束。所以正确使用方法,应该是传入ApplicationContext,避免Toast导致内存泄漏
            mToast = Toast.makeText(context.getApplicationContext(), null, duration);  //小米手机首次弹出应用名
            mToast.setText(text);
        }
        mToast.show();
    }

    public static void show(Context context, int resId, Object... args) {
        show(context, String.format(context.getResources().getString(resId), args), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, String format, Object... args) {
        show(context, String.format(format, args), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration, Object... args) {
        show(context, String.format(context.getResources().getString(resId), args), duration);
    }

    public static void show(Context context, String format, int duration, Object... args) {
        show(context, String.format(format, args), duration);
    }

//自定义Toast
    public static void show(Context context, String titles, String messages) {
        if (context == null)return;
         LayoutInflater inflater = LayoutInflater.from(context.getApplicationContext());
         View view = inflater.inflate(R.layout.toast_style, null);
         TextView title = view.findViewById(R.id.tv_title);
         title.setText(titles); //toast的标题
         TextView text = view.findViewById(R.id.tv_award);
         text.setText(messages); //toast内容
         Toast toast = new Toast(context.getApplicationContext());
         toast.setGravity(Gravity.CENTER, 12, 20);//setGravity用来设置Toast显示的位置,相当于xml中的android:gravity或android:layout_gravity
         toast.setDuration(Toast.LENGTH_LONG);//setDuration方法:设置持续时间,以毫秒为单位。该方法是设置补间动画时间长度的主要方法
         toast.setView(view); //添加视图文件
         toast.show();
    }

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值