Android自訂Toast的時間長度、位置、及顯示的View

來源:互聯網
上載者:User

標籤:

Android自訂Toast

首先是自訂時間長度:

說是這麼說,但是android內建的兩個時間長度 LENGTH_SHORT (2秒) 和LENGTH_LONG (3.5秒)基本已經夠用了,一般也沒有特地去設定幾十秒的Toast吧,這樣的話,還不如直接弄一個Dialog來的直接。

我們先看看如何讓Toast在3.5秒內自訂顯示長度:

    public static void showShort(Context context, String msg, int duration) {        final Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);        toast.show();        new Handler().postDelayed(new Runnable() {            public void run() {                toast.cancel();            }        }, duration);    }

至於3.5秒之上的自訂時間長度,那麼就可以通過休眠線程來實現

    /**     * @param context  傳遞的上下文     * @param msg      要顯示的文本資訊     * @param duration 自訂時間長度     */    public static void show(Context context, String msg, final int duration) {        final Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);        final long startTime = System.currentTimeMillis();        toast.show();        new Handler().postDelayed(new Runnable() {            public void run() {                try {                    //線程開始休眠                    Thread.sleep((long) (duration));                } catch (InterruptedException e) {                    e.printStackTrace();                }                long entTime = System.currentTimeMillis();                Log.i("ToastUtil", "------>>>顯示的時間為:" + (entTime - startTime));            }            //這100毫秒只是用來讓toast從隱藏到顯示所給的時間,不然直接休眠線程的話,toast是無法顯示出來的        }, 100);    }

顯示的時間長度如:10.191秒

這種方式基本別用,這可是直接讓UI線程進行休眠啊,除非作用一些特殊場所,不然還是別用;如果實在還是要用,建議還是弄個Dialog自訂View去仿製一個Toast,這樣自己想顯示多長時間就顯示多長時間。

接下來 自訂View 和 自訂位置就一起說了

首先是布局檔案 R.layout.toast_view:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:gravity="center"    android:background="@null"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:id="@+id/tv_message_toast"        android:textColor="#fff"        android:maxWidth="200dp"        android:padding="15dp"        android:textSize="18sp"        android:gravity="center"        android:drawableLeft="@drawable/btn_delete_red_circle"        android:background="@drawable/shape_toast_bg"        android:layout_height="wrap_content" /></LinearLayout>

接著是Toast的方法

    public static void show(Context context, String msg) {        Toast toast = new Toast(context);        //設定Toast顯示位置,置中,向 X、Y軸位移量均為0        toast.setGravity(Gravity.CENTER, 0, 0);        //擷取自訂視圖        View view = LayoutInflater.from(context).inflate(R.layout.toast_view, null);        TextView tvMessage = (TextView) view.findViewById(R.id.tv_message_toast);        //設定文本        tvMessage.setText(msg);        //設定視圖        toast.setView(view);        //設定顯示時間長度        toast.setDuration(Toast.LENGTH_LONG);        //顯示        toast.show();    }

在Activity調用一下ToastUtil.show(this, "這是一個自訂視圖和位置的Toast");之後,顯示的如下:

布局可以自訂,雖然這個Toast有點醜,但是讓大家知道怎麼弄一個自訂Toast了。

Android自訂Toast的時間長度、位置、及顯示的View

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.