android 自定义Toast

来源:互联网 发布:数据分析有发展前途 编辑:程序博客网 时间:2024/05/20 00:16

    现在很多的应用 都会自定义个Toast.的方式

     Toast 本身的代码是很长的,有时间可以看看没有 时间 可以不看 。Toast 的两个方法是自定义Toast的重点  一个就是 setView(),setGravity(); 有这两个就够了  一个是 设置View 一个是设置位置 这样的话 你就可以通过 这两个方法的组合进行 好看的Toast。

  我选择的是继承Toast 重新定义了一个自己的Toast  比较简单 贴代码吧,根据这一层可以再进一步的封装 ,可以在子线程中 直接调用使用。 new Handler(Looper.getMainLooper);  这样就获取到主线程的handler 进行通信。自己封装吧。 


public final class CustomToast extends Toast {public TextView tipInfo;public ImageView tipImage;public MCCustomToast(Context context) {super(context);View view = LayoutInflater.from(context).inflate(R.layout.toast_style_center, null);this.setView(view);this.tipInfo = (TextView) view.findViewById(R.id.jd_toast_txt);this.tipImage = (ImageView) view.findViewById(R.id.jd_toast_image);this.setGravity(17, 0, 0);}public final void setType(int type) {if (this.tipImage != null) {switch (type) {case 1: {this.tipImage.setBackgroundResource(R.drawable.toast_exclamation);return;}case 2: {this.tipImage.setBackgroundResource(R.drawable.toast_tick);return;}}}}public final void setText(CharSequence text) {if (this.tipInfo != null) {this.tipInfo.setText(text);}}}








0 0
原创粉丝点击