相信大写程序时都会用到Toast给用户一些提示,当然android本身就有Toast提示,但是我们时常都会对他做一些处理,那么我就下面贴出我封装的好Toast;

public class ToastUtils {    public static void show(Context context, CharSequence msg) {        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();    }    public static void onMiddleToast(Context context, CharSequence msg) {                if (context == null || TextUtils.isEmpty(msg))            return;        /**         * 尝试解决Android7.1.1 android.view.WindowManager$BadTokenException         */        try{            Toast toast = Toast.makeText(context,msg, Toast.LENGTH_SHORT);            toast.setGravity(Gravity.CENTER_VERTICAL , 0, 0);            View view = toast.getView();            view.setBackgroundResource(R.drawable.toast_bg);            view.setPadding(40, 20, 40, 20);            TextView v = (TextView) view.findViewById(android.R.id.message);            v.setTextSize(12);            v.setTextColor(context.getResources().getColor(android.R.color.white));            toast.setView(view);            toast.show();        }catch (Exception e){        }    }    public static void onMiddleToast(Context context, @StringRes int stringId) {        Toast toast = Toast.makeText(context,stringId, Toast.LENGTH_SHORT);        toast.setGravity(Gravity.CENTER_VERTICAL , 0, 0);        View view = toast.getView();        view.setBackgroundResource(R.drawable.toast_bg);        view.setPadding(40, 20, 40, 20);        TextView v = (TextView) view.findViewById(android.R.id.message);        v.setTextSize(12);        v.setTextColor(context.getResources().getColor(android.R.color.white));        toast.setView(view);        toast.show();    }    public static void show(Context context, @StringRes int stringId) {        Toast.makeText(context, stringId, Toast.LENGTH_LONG).show();    }    public static void show(View view, CharSequence msg) {        show(view.getContext(), msg);    }    public static void show(View view, @StringRes int stringId) {        show(view.getContext(), stringId);    }}

toast时的背景我也做了处理,怎么自定义也不知道大家知不知道,我就在这里啰嗦一下,直接附图啦


下面我贴出我的代码,大家直接贴上自己的工程中就可以用啦

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#c3000000"/>    <corners android:radius="5dp"/>shape>

更多相关文章

  1. log函数封装,让WIN32和android ndk共用相同的log函数,方便调试
  2. Android(安卓)Studio 中 FAILURE: Build failed with an excepti
  3. android项目的出现小红叉,如何重构
  4. RK3288 android7.1.2 插 UVCCamera 摄像头, android studio 调试9
  5. linux32位下使用Android提示 ERROR: 32-bit 解决办法
  6. Android中设计模式无处不在之单例模式
  7. Android仿iOS启动界面
  8. Android(安卓)Studio 自动代码提示设置
  9. 46.Android(安卓)自定义Dialog

随机推荐

  1. kotlin笔记 一
  2. Android中通过Messenger与Service实现进
  3. 浅谈Android事件分发机制
  4. android字符太长自动变成省略号
  5. Android的源代码结构
  6. Android(安卓)UI之ImageButton(带图标的
  7. Learning Android(安卓)第1章 - 工欲善其
  8. Android进程的内存管理分析
  9. Android项目优化宝典
  10. android进程在管理器中被杀死