---
title: "创建消息通知(Creating  Toast Notification)"
author: "ipFire"
date: "2011-04-16T10:39:15Z"
tags: []
categories: ["移动开发"]
slug: "creating-toast-notification"
draft: false
lastmod: "2011-04-16T10:41:40Z"
id: 1004842
url: "https://ipfire.iteye.com/blog/1004842"
---

**创建消息通知(Creating  Toast Notification)**

 

一个消息通知是在窗口表面弹出的消息,它只填充消息所的空间,并且用户当前的activity仍然是可见和可交互的.这个通知会自动的淡入淡出.而且不接爱交互事件.

 

下面的截图显示了一个来自闹钟程序的消息通知的例子.一旦闹钟开启, 显示一个通知来提醒你闹钟已经设置.

![](/upload/picture/pic/87651/3c426baf-4064-34ac-884a-b33d99ba33af.png)

 

 

一个消息可以在Activity或服务里被创建和显示.如果你在一个服务里面创建一个消息提醒,它显示在当前焦点的Activity的前面.

**基础(The Basics)**

首先:通过其中一个makeText() 方法实例化一个Toast对象,该方法接受三个参数:应用程序上下文,文字信息和这个Toast的持续时间.它返回一个正确地实例化对象.你可以通过show()方法显示这个消息提醒,如下面的例子显示一样:

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

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

 

 

这个例子展示了你需要的大部分消息提醒的一切,你将很少需要其它东西. 你可以定位不同位置的消息提醒,甚至使用你自己的布局代替简单的文本信息.接下来的章节说明你如何做这些事情.

 

你也可以链式你的方法,避免绑定你的消息对醒对象.像这样:

 

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

 

 

 

**定位消息提醒(Positioning your Toast)**

一个标准的消息提醒显示在靠近屏幕的下方.水平居中.你可以通过 setGravity(int, int, int)方法修改它的位置.该方法接受三个参数:引力常量,x位置偏移,y位置偏移.

 

例如:你希望这个消息提醒将显示在左上角,你可以像这样设置引力.

 

 

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

 

 如果你想向右推动位置,增加第二个参数的数值,向下推动,   增加最后一个参数的数值.

 

**创建自定义Toast(Creating a Custom Toast View)**

如果一个简单的文本信息不够,你可以为你的消息提醒创建自定义的布局.创造自定义布局,在XML或你的应用程序里面定义一个View布局,然后把根节点的View对象传入setView(View)方法.

 

例如:你可以为toast创建像右边截图看见的布局，XML文件如下(保存为toast\_layout.xml)

 

```
<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>
```

 

 

注意这个LinearLayout元素的ID是“toast\_layout\_root”，你必须使用这个ID生成布局，如下所示:

 

```
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对象。

所于现在获取并定义这个ImageView 和TextView 元素的内容.最后， 通过Toast(Context)方法创建一个新的Toast,并为这个Toast设置一些属性。

例如重力和间隔，然后调用setView(View) 并传入生成的布局。现在你可以调用show()显示你自定义布局的Toast了.

 

 

```
注：不要为Toast 使用公共的构造方法，除非你打算根据setView(View)定义布局。如果你没有自定义布局使用，你必须使用 makeText(Context, int, int)创建Toast.
```

 

参考：/android-sdk-windows/docs/guide/topics/ui/notifiers/notifications.html