星空网 > 软件开发 > 操作系统

Android自定义toast

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

 

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

 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();  }    

2、接着设置布局样式:

  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;   //下间距  }

 

3、自定义toast弹出风格 动画的效果 。  toast_styles.

<??><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>

toast_enter.
<??><set ="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>

toast_exit.
<??><set ="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>

 

 

4、common_toast.

<??><LinearLayout ="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>

 

以上,这样就得到了一个自定义的带动画效果的view容器了。然后,怎么调用呢? 别急,我们需要再写个方法。。

  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));  }

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

然后,在其他类中,使用

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

 

效果如下:

Android自定义toastimages/loading.gif' data-original="http://images.cnitblog.com/blog2015/478768/201505/041726076261465.gif" width="308" height="494" />

 




原标题:Android自定义toast

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

印尼Jackmall多元化综合分销平台,解析跨境出海新航道!:https://www.ikjzd.com/articles/93882
推荐3种方法:快速查找eBay已上线刊登物品!:https://www.ikjzd.com/articles/93884
wish卖家的10道必答题!:https://www.ikjzd.com/articles/93886
不加入会员就延迟入仓?亚马逊又推新计划!:https://www.ikjzd.com/articles/93889
亚马逊秒杀技巧!亚马逊小白必备:https://www.ikjzd.com/articles/93891
商标注册难度越来越大,买卖商标将成常态?:https://www.ikjzd.com/articles/93892
天坛最佳攻略 天坛必玩景点:https://www.vstour.cn/a/408240.html
央视新址为什么会找回:https://www.vstour.cn/a/408241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流