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);
}
}
}
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);
}
}
}
相关文章推荐
- Android Spinner In Toolbar
- Android中获取LayoutInflater的三种方法以及区别
- 在android studio 中使用SlidingMenu依赖库方法
- Android的Fragment中onActivityResult不被调用的终极解决方案
- Android目录结构介绍&Android学习之hello world
- Android apk反编译
- Android studio "社会化分享"实现步骤(总结mob网步骤)
- android.graphics.Camera
- Android Studio 创建Java工程
- Android4.0 Android5.0 Android Wear开发文档
- Android使用广播(BroadCast)实现强制下线的方法
- android Message Handel机制 thread
- Android 5.0 API新增和改进
- java Android 中关于线程同步问题
- Android Studio stuck on “Gradle: resolve dependancies '_debugCompile'” or 'detachedConfiguration1'
- 简单定制Android控件(3) - 打造通用的PopupWindow(二)
- Android碎片化难题:手游兼容性测试应该适配哪些机型?
- Android xUtils 上传文件到C# webservice 服务器
- android 系统设置中的应用详情
- 【同行说技术】Android图片处理技术资料汇总(一)