標籤:
使用Android預設的Toast
Toast簡介:
Toast是一個簡單的訊息顯示框,能夠短暫的出現在螢幕的某個位置,顯示提示訊息。
預設的位置是螢幕的下方正中,一般Toast的使用如下:
Toast.makeText(this,"1222222",Toast.LENGTH_SHORT).show();
Toast是static修飾的靜態類,意味著可以直接使用,所以可以不用建立對象直接調用makeText方法,
該方法需要傳入三個參數:
/** * Make a standard toast that just contains a text view. * * @param context The context to use. Usually your {@link android.app.Application} * or {@link android.app.Activity} object. * @param text The text to show. Can be formatted text. * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or * {@link #LENGTH_LONG} * */
第一個參賽數當前context,第二個是需要顯示的常值內容,第三個參數是顯示時間
但這裡的顯示時間只有兩種,一個是 Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG. 顧名思義,後者比前者要長一點。
自訂Toast自訂圖片
今天看到某音樂播放軟體有個收藏功能會彈出類似效果的Toast
上面一顆紅♥?,下面顯示常值內容, 那麼這個效果如何?呢?
在開啟Toast 源碼可以看到一個方法setView
/** * Set the view to show. * @see #getView */ public void setView(View view) { mNextView = view; }
想必可以通過該方法添加圖片和文本
那接下來就可以嘗試自訂一個布局檔案,並把該布局通過setView的方式添加到Toast裡面
布局檔案為線型布局,內容如下,添加一個現形布局,在該線型布局中添加一個ImageView和一個TextView
該布局檔案名稱為toast_view.xml,設定orientation為vertical為垂直排列,並將準備好的心型圖片設定為ImageView的背景
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/black" android:gravity="center" android:minWidth="100dp" android:orientation="vertical"> <!--android:background="@drawable/toast_bg"--> <ImageView android:id="@+id/toast_image" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:layout_margin="2dp" android:background="@drawable/redheart" /> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:gravity="center" android:text="" android:textColor="#ffffff" android:textSize="15dp" /> </LinearLayout></LinearLayout>
結下來建立一個ToastView Class,把該布局檔案關聯起來
/** * * @param context * @param text */ public ToastView(Context context, String text) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.toast_view, null); TextView t = (TextView) view.findViewById(R.id.toast_text); t.setText(text); if (toast != null) { toast.cancel(); } toast = new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); }
通過setText方法把要顯示的文本顯示出來
當然還可以進一步最佳化,把ImageView的背景替換掉
public ToastView(Context context, String text) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.toast_view, null); ImageView imageView=(ImageView)view.findViewById(R.id.toast_image); imageView.setBackgroundResource(R.mipmap.ic_launcher); TextView t = (TextView) view.findViewById(R.id.toast_text); t.setText(text); if (toast != null) { toast.cancel(); } toast = new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); }
通過這個方法,先擷取到Layout然後通過findViewById擷取到子控制項進行設定
然而這樣的效果依然不是我們想要的,顯示出來並不是帶圓角的
這個時候就需要添加一個shape布局
設定圓角,並把該shape添加到LinearLayout的背景
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#c83e3e3e" /> <!--radius shape--> <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:radius="8dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /></shape>
自訂位置
那麼如何自訂顯示位置?
通過查看Toast的源碼可以看到一個setGravity的方法,是專門用來設定Toast的位置
/** * Set the location at which the notification should appear on the screen. * @see android.view.Gravity * @see #getGravity */ public void setGravity(int gravity, int xOffset, int yOffset) { mTN.mGravity = gravity; mTN.mX = xOffset; mTN.mY = yOffset; }
該方法有三個參賽,第一個是整形類型的gravity,該參數設定具體的位置,可以參考Gravity類
一般常用的有:
Gravity.CENTER
Gravity.LEFT
Gravity.RIGHT
Gravity.TOP
Gravity.BOTTOM
顧名思義,第二個和第三個參數是位移量,針對第一個參數的位移量
所以,如果設定Toast在螢幕正當中,只需要這樣
toast.setGravity(Gravity.CENTER, 0, 0);
自訂Toast的顯示時間
未完待續。。。。。。
Android帶圖片的Toast(自訂Toast)