实现类似塔读掌阅的Toast

来源:互联网 发布:如何成为首富 知乎 编辑:程序博客网 时间:2024/05/24 01:48

开发过程中,提示肯定会用到。系统提供的简单toast方便,但是样式真的很挫。一个好的应用如果toast这类细节没有做到的法,真是有些大煞风景的。

toast系统已经给我们提供提示的类,查看系统提供的toast还支持我们自己的View.好,咱们就用系统提供的这方法 来实现自定义的toast.

1.上图
这里写图片描述
2.代码实现

public class MsgView {    private static MsgView msgView;    private MsgView(){}    public static MsgView getInstance(){        if(msgView == null){            msgView = new MsgView();        }        return msgView;    }    public void show(Context context,String msg){        if(Looper.myLooper() == Looper.getMainLooper()){            //还是有必要对线程进行判断的 只有在主线程中才能真正的调用,子线程直接就会奔溃的            Toast result = new Toast(context);            LayoutInflater inflate = (LayoutInflater)                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            View v = inflate.inflate(R.layout.view_msg,null);            Animation animation = AnimationUtils.loadAnimation(context,R.anim.anim_msg);            v.startAnimation(animation);            TextView tv = (TextView)v.findViewById(R.id.text_msg);            tv.setText(msg);            result.setView(v);            result.setDuration(Toast.LENGTH_SHORT);            result.show();        }else{            Log.e("wuzp","toast is not in mainThread call");        }    }}

3.demo的下载地址
git@github.com:wzp09tjlg/ToastDemo.git

原创粉丝点击