Android自訂toast

來源:互聯網
上載者:User

標籤:

在開發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.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>
toast_enter.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>

toast_exit.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>

 

 

4、common_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"    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自訂toast

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.