Android自定义toast – 飞天小鳄 – 博客园

来源: Android自定义toast - 飞天小鳄 - 博客园

在开发Android应用时,一般我们都会用toast来弹出提示消息,简单高 效。但是在不同的手机下toast显示的位置和样式可能会不同,而且系统自带的toast样式奇丑(个人觉得...),那么怎样定制一个个性的toast 提示框呢。。。  今天我就分享一下自己写的自定义toast,不足之处还请大家多多指点。(后边有效果图)

 

1、因为toast的特性,所以我们定义toast为单例模式。

[code]

复制代码
private static ZToast instance; //单例的
    private View mToastView;//自定义toast view
    private TextView mTextView;
    private Boolean mIsShow;//记录状态 是否在显示
    private Timer mTimer;//定时器


    public synchronized static ZToast getInstance(Context context) {
        if (instance == null)
            instance = new ZToast(context);
        return instance;
    }    

    private ZToast(Context context) {
        mIsShow = false;// 记录当前Toast的内容是否已经在显示

     //这里初始化toast view
        mToastView = LayoutInflater.from(context).inflate(R.layout.common_toast, null);
                
     //用来提示的文字
        mTextView = ((TextView) mToastView.findViewById(R.id.toast_text));

     //初始化计数器
        mTimer = new Timer();
        // 设置布局参数
        setParams();
    }        
[/code]
2、接着设置布局样式:
[code]
    private LayoutParams mParams;
    private void setParams() {
        mParams = new WindowManager.LayoutParams();//初始化
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  //高
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;   //宽
        mParams.format = PixelFormat.TRANSLUCENT;               
        mParams.windowAnimations = R.style.custom_animation_toast;// 设置进入退出动画效果
        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        mParams.gravity = Gravity.BOTTOM;        //对其方式
        mParams.y = 45;      //下间距
    }
[/code]
3、自定义toast弹出风格 动画的效果 。  toast_styles.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="custom.animation.toast" parent="@android:style/Animation.Toast">
        <item name="android:windowEnterAnimation">@anim/toast_enter</item>
        <item name="android:windowExitAnimation">@anim/toast_exit</item>
    </style>
</resources>
复制代码[/xml]
toast_enter.xml
复制代码[xml]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="1"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="85" />
<translate
android:duration="350"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@interpolator/accelerate_quad"
android:toXDelta="0"
android:toYDelta="-105" />

<alpha
android:duration="100"
android:fromAlpha="0"
android:toAlpha="1" />

<translate
android:duration="80"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="350"
android:toXDelta="0"
android:toYDelta="20" />

</set>

[/xml]
toast_exit.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="500"
android:fromYDelta="0"
android:interpolator="@interpolator/accelerate_quad"
android:toYDelta="50%p" />

<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

</set>

[/xml]
4、common_toast.xml
[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"
android:background="@drawable/bg_common_toast"
android:orientation="horizontal" >

<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left|center"
android:textColor="@android:color/black"
android:textSize="@dimen/toast_font_size" />

</LinearLayout>

[/xml]
以上,这样就得到了一个自定义的带动画效果的view容器了。然后,怎么调用呢? 别急,我们需要再写个方法。。
[code]
    public void show(String text, int mShowTime) {
if (mIsShow) {// 如果Toast已经在显示 就先给隐藏了
if (ManageApp.mWdm != null && mToastView != null)
ManageApp.mWdm.removeView(mToastView);
// 取消计时器
if (mTimer != null) {
mTimer.cancel();
mTimer = new Timer();
}
}
//设置显示内容
mTextView.setText(text);
//设置显示状态
mIsShow = true;
// 将其加载到windowManager上
ManageApp.mWdm.addView(mToastView, mParams);

//设置计时器
mTimer.schedule(new TimerTask() {
@Override
public void run() {
ManageApp.mWdm.removeView(mToastView);
mIsShow = false;
}
}, (long) (mShowTime == Toast.LENGTH_LONG ? 2200 : 1200));
}

[/code]

大家会问mWdm是个神马?  其实它就是WindowManager(public static WindowManager mWdm;),最终还是需要使用它来吧view显示在屏幕上的。我们把它定义在程序的Application类中,并在oncreate()里初始化 mWdm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 这样就能保证他的生命周期会比我们的activity长,从而在执行计时器的时候不会报各种各样的异常。(如果有其他更好的办法,望告知。)

然后,在其他类中,使用

[code]

ZToast.getInstance(mContext).show("我是自定义的toast",Toast.LENGTH_LONG );

[/code]

效果如下: