该自定义Toast用到了定时器和PopupWindow,鉴于Popupwindow在7.0以上位置显示有异常,所以先解决该问题。
public class FixedPopupWindow extends PopupWindow {
public FixedPopupWindow(View contentView, int width, int height) {
super(contentView, width, height, false);
}
@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
}
在知道如何兼容7.0以上系统了之后,那么久开始自定义Toast。
public class CustomToast {
private FixedPopupWindow popupWindow;
private TimeCount count;
private ViewGroup rootView;
private int totalTime;
private int perTime;
public CustomToast(Context context, ViewGroup viewGroup)
{
totalTime=3000;
perTime=1000;
if(popupWindow==null)
{
this.rootView=viewGroup;
View view= LayoutInflater.from(context).inflate(R.layout.custom_toast,null,false);
popupWindow=new FixedPopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//remove timer when the popupwindow dismisses
count=null;
}
});
}
}
//为自定义Toast设置动画 这里主要是设置了淡入淡出的效果
public void setAnimation(int animationStyle)
{
popupWindow.setAnimationStyle(animationStyle);
}
//展示自定义的Toast
public void show()
{
popupWindow.showAtLocation(rootView, Gravity.CENTER,0,0);
count=new TimeCount(totalTime,perTime,new TimeCount.Timercallback()
{
@Override
public void onFinish() {
popupWindow.dismiss();
popupWindow=null;
}
});
count.start();
}
//设置TextView的背景
public void setBackground(GradientDrawable drawable)
{
TextView textView=popupWindow.getContentView().findViewById(R.id.tv_show);
//判断一下样式
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
textView.setBackground(drawable);
}else{
textView.setBackgroundDrawable(drawable);
}
}
//设置TextView的背景 传入的是资源id
public void setBackground(int resId)
{
TextView textView=popupWindow.getContentView().findViewById(R.id.tv_show);
textView.setBackgroundResource(resId);
}
//设置TextView的padding值
public void setPadding(int paddingLeftValue,int paddingTopValue,int paddingRightValue,int paddingBottomValue)
{
TextView textView=popupWindow.getContentView().findViewById(R.id.tv_show);
textView.setPadding(paddingLeftValue,paddingTopValue,paddingRightValue,paddingBottomValue);
}
//设置TextView的文字显示
public void setText(String str)
{
TextView textView=popupWindow.getContentView().findViewById(R.id.tv_show);
textView.setText(str);
}
//设置自定义Toast显示的时间
public void setDuringTime(int time)
{
this.totalTime=time;
}
//设置定时器回调的最短时间
public void setPerTime(int time)
{
this.perTime=time;
}
}
自定义Toast用到的布局如下:
custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88000000"
android:textColor="#ffffff"
android:text="测试"/>
</LinearLayout>
调用的演示:
CustomToast customToast=new CustomToast(MainActivity.this,rootView);
customToast.setAnimation(R.style.popwin_anim_style);
GradientDrawable drawable=new GradientDrawable();
drawable.setCornerRadius(20);
drawable.setColor(Color.argb(88,0,0,0));
customToast.setBackground(drawable);
customToast.setText(getResources().getString(R.string.correct_toast));
customToast.setDuringTime(3000);
customToast.setPerTime(1000);
customToast.setPadding(50,20,50,20);
customToast.show();
中间用到的动画以及Style如下:
<style name="popwin_anim_style">
<item name="android:windowEnterAnimation">@anim/menushow</item>
<item name="android:windowExitAnimation">@anim/menuhide</item>
</style>
menushow.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="500">
</alpha>
menuhide.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="500">
</alpha>
可以肯定,实现得非常粗糙。
想学习交流唠嗑的,可以加QQ群:106510493。