您的位置:首页 > 移动开发 > Android开发

android 自定义Toast

2016-01-15 15:18 525 查看
    现在很多的应用 都会自定义个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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: