toast

来源:互联网 发布:nginx 线程模型 编辑:程序博客网 时间:2024/05/17 04:42

ToastCompat

欢迎各位提出改进意见及完善,欢迎star.

  • 做这个的出发点是不管是测试还是本身的项目需求,需要搞一个toast,满足各式各样的需要,大概有这些,于是就有写了
    • 有需求的单列的toast
    • 有需求可以自定义弹出时间的toast
    • 有需求在关闭系统的通知权限后照样可以弹出toast
    • 有需求可以弹出自定义view
  • 参考资料
    • 参考toast的源码,系统维护是一个队列,同时也用到了aidl
    public void show() {        if (mNextView == null) {            throw new RuntimeException("setView must have been called");        }        INotificationManager service = getService();        String pkg = mContext.getOpPackageName();        TN tn = mTN;        tn.mNextView = mNextView;        try {            service.enqueueToast(pkg, tn, mDuration);        } catch (RemoteException e) {            // Empty        }    }


  • 参考okhttp源码,以前没有太在意,后来发现,不管是okhttp中的builder还是dialog中的,他们的功能都是设置参数,这种思想非常好
  • 使用方法
    • 直接拷贝源码到项目中,简单暴力直接,有点重口味了,同时源码也不多。
    • 直接引入库
      compile ‘com.gs:ToastCompat:1.0.0’

具体调用api方法
1.单列
new CustomToast.Builder(this).setIsSingle(true).setText("你好").build()
.show();

2.自定义时间
new CustomToast.Builder(this).setText("你好").setDuration(2000).build()
.show();

3.自定义View
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
new CustomToast.Builder(this).setIsSingle(true).setView(imageView).setDuration(2000).build()
.show();

4.复合使用
new CustomToast.Builder(this).setIsSingle(true).setText("你好").setDuration(2000).build()
.show();

  • 效果展示
  • Thanks
    [rongwu](https://github.com/rongwu/ToastCompat
    项目地址
  • 0 0
    原创粉丝点击