Android開發者指南-Toast Notifications[原創譯文]

User Interface

--Notifying the User

--Creating Toast Notifications

 

版本:Android 3.2

 

快速查看

Toast是一種只在屏幕上顯示一小會兒的消息,它沒有焦點(也不暫停當前的activity),因此也不能接受用戶的輸入。

可以通過定製toast的佈局來顯示圖片。

 

在本文中:

基礎知識

定位Toast

創建自定義Toast視圖

 

關鍵類

Toast

 

toast通知是一種在窗口表面彈出的消息。它只佔用信息顯示所需的空間,用戶當前的activity仍保持可見並可交互。該通知自動實現淡入淡出,且不接受人機交互事件。

 

以下截圖展示了鬧鐘程序的toast通知示例。一旦鬧鐘被打開,就會顯示一條toast作爲對設置的確認。

Android開發者指南-創建Toast通知[原創譯文]

 

toast能被ActivityService創建並顯示。如果由Service創建,則toast會顯示在當前已獲得焦點的Activity前面。

 

如果需要用戶對通知進行響應,可以考慮使用Status Bar Notification

 

基礎知識

首先,用某個makeText()方法來實例化一個Toast對象。該方法有三個參數:應用程序上下文Context、文本信息和toast的持續顯示時間。它將返回一個已正確初始化的Toast對象。可以用show()方法來顯示該toast通知,示例如下:

Context context = getApplicationContext(); 

CharSequence text ="Hello toast!"; 

int duration =Toast.LENGTH_SHORT; 

Toast toast =Toast.makeText(context, text, duration); 

toast.show();

上例演示了大部分toast通知需要的所有內容,應該不大會需要用到其他內容了。不過,你也許想在其他位置顯示toast或是要用自己的佈局替換默認相對簡單的文本消息,下一節將描述如何完成。

還可以將多個方法鏈接起來寫,以避免持久化Toast對象,就像這樣:

Toast.makeText(context, text, duration).show();

 

定位Toast

標準的toast通知左右居中地顯示在屏幕底部附近。可以通過setGravity(int, int, int)方法來改變顯示位置。它接受三個參數:重力常量常數GravityX方向偏移和Y方向偏移。

例如,如果決定把toast置於左上角,可以這樣設置重力常數:

toast.setGravity(Gravity.TOP|Gravity.LEFT,0,0);

 

如果想讓位置向右移,就增加第二個參數的值;要向下移,就增加最後一個參數的值。

 

創建自定義的Toast視圖

 

如果不滿足於簡單的文本消息,還可以爲toast通知創建一個自定義佈局。要創建自定義佈局,需要用XML或程序代碼定義一個View佈局,然後把根View對象傳給setView(View)方法。

 

例如,可以用以下的XML(保存爲toast_layout.xml)創建出以下截圖中所示的佈局:

  Android開發者指南-創建Toast通知[原創譯文]

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 

              android:id="@+id/toast_layout_root" 

              android:orientation="horizontal" 

              android:layout_width="fill_parent" 

              android:layout_height="fill_parent" 

              android:padding="10dp" 

              android:background="#DAAA" 

              > 

    <ImageViewandroid:id="@+id/image" 

               android:layout_width="wrap_content" 

               android:layout_height="fill_parent" 

               android:layout_marginRight="10dp" 

               /> 

    <TextViewandroid:id="@+id/text" 

              android:layout_width="wrap_content" 

              android:layout_height="fill_parent" 

              android:textColor="#FFF" 

              /> 

</LinearLayout>

注意,LinearLayout元素的ID“toast_layout”。必須用這個IDXML中解析出佈局,如下:

LayoutInflater inflater = getLayoutInflater(); 

View layout = inflater.inflate(R.layout.toast_layout, 

                               (ViewGroup)findViewById(R.id.toast_layout_root)); 

 

ImageView image =(ImageView) layout.findViewById(R.id.image); 

image.setImageResource(R.drawable.android); 

TextView text =(TextView) layout.findViewById(R.id.text); 

text.setText("Hello! This is a custom toast!"); 

 

Toast toast =newToast(getApplicationContext()); 

toast.setGravity(Gravity.CENTER_VERTICAL,0,0); 

toast.setDuration(Toast.LENGTH_LONG); 

toast.setView(layout); 

toast.show();

首先,用getLayoutInflater()(或getSystemService())來讀取LayoutInflater,然後用inflate(int, ViewGroup)將佈局(layout)從XML中解析出來。第一個參數是layout資源ID,第二個參數是根View。可以用解析出來的layout獲取其他View對象,之後獲取並定義ImageViewTextView元素的內容。最後,用Toast(Context)創建一個新的toast,設置一些屬性如gravityduration等。然後調用setView(View)並將解析出的layout傳入。現在就可以調用show()來顯示自定義佈局的toast了。

 

注意:除非想用setView(View)來定義佈局,否則不要用公共構造方法來構造Toast。如果沒有可用的自定義佈局,則必須使用makeText(Context, int, int)來創建Toast

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章