Android Notifications通知詳解(1)

來源:互聯網
上載者:User

一、Toast Notifications

以背景改變方式,提示一些簡短的訊息,訊息視窗自動淡入淡出,不接受互動事件。

例如:當下載某個檔案完成時,可以提示簡短的“儲存成功”。

顯示效果:

建立彈出提示方法:

1、建立Toast對象,可以通過Toast提供的靜態方法makeText(Context context, String message, int duration)

context:應用內容物件,這裡可以傳遞getApplicationContext()

message:提示文本

duration:顯示時間長度,可以使用Toast.LENGTH_SHORT、Toast.LENGTH_LONG

 
  1. Context context = getApplicationContext(); 
  2.  
  3. Toast toast = Toast.makeText(context, "儲存成功", Toast.LENGTH_LONG); 

2、顯示提示,調用show()方法

 
  1. toast.show(); 

上述兩步也可簡寫為:

 
  1. Toast.makeText(getApplicationContext(), "儲存成功", Toast.LENGTH_LONG).show(); 

這樣,最簡單的提示資訊已經完成。現在來看看如何建立自訂外觀Toast notification。

3、自訂外觀Toast通知

3.1、定義XML資源檢視作為提示的外觀

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.               android:id="@+id/toast_layout_root" 
  4.               android:orientation="horizontal" 
  5.               android:layout_width="fill_parent" 
  6.               android:layout_height="fill_parent" 
  7.               android:padding="10dp" 
  8.               android:background="#DAAA" 
  9.               > 
  10.     <ImageView android:id="@+id/image" 
  11.                android:layout_width="wrap_content" 
  12.                android:layout_height="fill_parent" 
  13.                android:layout_marginRight="10dp" 
  14.                android:src="@drawable/icon" 
  15.                /> 
  16.     <TextView android:id="@+id/text" 
  17.               android:layout_width="wrap_content" 
  18.               android:layout_height="fill_parent" 
  19.               android:textColor="#FFF" 
  20.               /> 
  21. </LinearLayout> 

其中TextView文本組件用來顯示需要提示的文本。這裡預設沒有設定文字。

3.2、解析上述XML資源檢視,並設定提示文本

 
  1. LayoutInflater inflater = getLayoutInflater();//XML資源布局填充對象 
  2. View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); 
  3.  
  4. //修改自訂布局中TextView文本,作為提示資訊 
  5. TextView textView = (TextView) layout.findViewById(R.id.text); 
  6. textView.setText("自訂介面:儲存成功"); 

3.3、建立Toast對象,並設定視圖、顯示視圖

 
  1. Toast toast = new Toast(getApplicationContext()); 
  2. //設定垂直置中,水平、垂直位移值為0,表示正中間。 
  3. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//設定提示框位置,三個參數分別代表:對其方式、水平位移值、垂直位移值。 
  4. toast.setDuration(Toast.LENGTH_LONG); 
  5. toast.setView(layout);//設定顯示的視圖 
  6. toast.show(); 

顯示:


聯繫我們

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