Android 使用者介面—廣播通知(Toast Notifications)

來源:互聯網
上載者:User

廣播通知(Toast Notifications)

廣播通知是在視窗表面彈出的一個訊息。它只填充訊息展現需要的空間,並且使用者當前的Activity依然可見和可互動。通知自動的漸入漸出,不接受互動事件。

下面圖1顯示一個例子是鬧鐘應用的廣播通知,一旦鬧鐘被開啟,就會在你設定的提醒時間顯示一個廣播通知。

圖1

廣播通知能夠由Activity或Service建立和顯示。如果你建立了一個源自Service的廣播通知,它會顯示當前有焦點的Activity的前面。

如要需要使用者對通知做出響應,請考慮使用狀態列通知。

基礎

首先,用makeText()方法執行個體化一個Toast對象。這個方法需要三個參數:1.應用程式的Context對象;2.要顯示的簡訊;3.通知持續表示的時間。這個方法會返回一個合適的被執行個體化的Toast對象。你能夠用show()方法顯示廣播通知,顯示方法如下:

Context context
= getApplicationContext();
CharSequence text
= "Hello toast!";
int duration
= Toast.LENGTH_SHORT;

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

這個樣本程式給你示範大多數廣播通知所需要做的每一件事情。你很少需要再做其他的事情。但是,你可能想要把廣播通知放到不同的位置,甚至要使用自己的布局來替代那個簡單的簡訊框。以下,將向你介紹如何?這些想法。

給廣播通知定位

標準的廣播通知水平置中顯示在螢幕底部附近,你能夠通過setGravity(int, int, int)方法來改變這個位置。這個方法有三個參數:1.Gravity常量(詳細參照Gravity類);2.X軸位移量;3.Y軸位移量。

例如,如果你想讓通知顯示在螢幕的左上方,你可以用下面這樣的方法調用:

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

如果你想要向右移動位置,可以增加第二個參數的值。要向下移動,可以增加最後一個參數的值。

建立一個定製的廣播視窗

如果一個簡單的簡訊不同滿足現實的需要,你可以給廣播通知建立一個定製的布局。要建立一個定製的布局,可以在XML檔案或應用程式代碼中定義一個View布局,然後把根View對象傳遞給setView(View)方法。

例如,你可以用下面的XML檔案建立一個如2所示的廣播通知視窗。

<LinearLayout
xmlns: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"
              >
    <ImageView
android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView
android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />

</LinearLayout>

圖2

注意,LinearLayout元素的ID屬性值是“toast_layout_root”。你必須使用這個ID的把XML的定義填充到布局中,方法如下:

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
= new
Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,
0,
0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

首先,用getLayoutInflater()方法(或getSystemService()方法)擷取LayoutInflater對象,然後使用inflate(int,
ViewGroup)方法把XML定義填充到布局中,這個方法的第一個參數是布局資源的ID,第二個參數要填充布局的View對象,本例是根View對象。你能夠使用這個被填充的布局來尋找布局中View對象,以便擷取和定義ImageView和TextView元素的內容。最後,用Toast(Context)方法建立一個廣播通知,並設定了一些廣播通知的屬性,如Gravity常量和持續顯示時間。然後調用setView(View)方法,把它傳遞給要填充的布局對象。然後調用show()方法顯示這個定製的廣播通知。

注意:除非你要用setView(View)方法定義布局,否則不要使用公用的Toast類構造器。如果不使用定製的布局,必須使用makeText(Context, int,
int)方法來建立廣播通知。

相關文章

聯繫我們

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