消息框可以在一个小型弹出式窗口中提供与操作有关的简单反馈。它只会填充消息所需的空间大小,并且当前 Activity 会一直显示及供用户与之互动。超时后,消息框会自动消失。
例如,点按电子邮件中的发送会触发“正在发送电子邮件…”消息框,如下面的屏幕截图所示:

消息框不可点击。如果需要用户对状态消息做出响应,请考虑使用通知。
基础知识
首先,使用 makeText()
方法之一实例化 Toast
对象。此方法接受三个参数:应用 Context
、文字消息和消息框时长。它会返回一个正确初始化的 Toast 对象。您可以使用 show()
显示消息框通知,如以下示例所示:
Kotlin
val text = "Hello toast!" val duration = Toast.LENGTH_SHORT val toast = Toast.makeText(applicationContext, text, duration) toast.show()
Java
Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
此示例演示了大多数消息框通知所需的一切。几乎不需要其他内容。但是,您可能需要以不同的方式放置消息框,甚至使用您自己的布局而不是简单的文字消息。下文介绍了如何执行这些操作。
您还可以链接方法,避免保留 Toast 对象,如下所示:
Kotlin
Toast.makeText(context, text, duration).show()
Java
Toast.makeText(context, text, duration).show();
放置消息框
标准消息框通知在屏幕底部附近水平居中显示。您可以使用 setGravity(int, int, int)
方法更改此位置。此方法接受三个参数:Gravity
常量、x 位置偏移和 y 位置偏移。
例如,如果您决定应将消息框显示在左上角,您可以如下设置重心:
Kotlin
toast.setGravity(Gravity.TOP or Gravity.LEFT, 0, 0)
Java
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果要向右移动位置,请增大第二个参数的值。如需向下移动,请增大最后一个参数的值。
创建自定义消息框视图
如果简单的文字消息不足够,您可以为消息框通知创建自定义布局。如需创建自定义布局,请在 XML 或应用代码中定义 View 布局,并将根 View
对象传递给 setView(View)
方法。
以下代码段包含消息框通知的自定义布局(另存为 layout/custom_toast.xml
):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_container" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>
请注意,LinearLayout 元素的 ID 是“custom_toast_container”。您必须使用此 ID 和 XML 布局文件“custom_toast”的 ID 膨胀布局,如下所示:
Kotlin
val inflater = layoutInflater val container: ViewGroup = findViewById(R.id.custom_toast_container) val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container) val text: TextView = layout.findViewById(R.id.text) text.text = "This is a custom toast" with (Toast(applicationContext)) { setGravity(Gravity.CENTER_VERTICAL, 0, 0) duration = Toast.LENGTH_LONG view = layout show() }
Java
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_container)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("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 对象,因此现在可以捕获并定义 ImageView 和 TextView 元素的内容。最后,使用 Toast(Context)
创建一个新消息框,并设置消息框的一些属性(例如重心和时长)。接着再调用 setView(View)
并向其传递膨胀后的布局。现在,您可以通过调用 show()
,使用自定义布局来显示消息框。
注意:除非您要使用 setView(View)
定义布局,否则请勿将公开构造函数用于消息框。如果您没有要使用的自定义布局,就必须使用 makeText(Context, int, int)
创建消息框。