---
title: "Android中Toast的用法简介"
author: "chunpeng"
date: "2009-11-11T14:53:32Z"
tags: []
slug: "android-toast"
draft: false
lastmod: "2009-11-11T14:53:32Z"
id: 514542
url: "https://chunpeng.iteye.com/blog/514542"
---

Toast是Android中用来显示显示信息的一种机制，和Dialog不一样的是，Toast是没有焦点的，而且Toast显示的时间有限，过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。  
首先建立一个ToastExample的项目，放置3个按钮，分别为Text Only，Icon Only，Text and Icon。布局及XML如下：  
![android-toast-usage-01](http://www.androidx.org/wp-content/uploads/2009/10/android-toast-usage-01.png "android-toast-usage-01")

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:gravity="center\_vertical"  
   android:layout\_width="fill\_parent"  
   android:layout\_height="fill\_parent"  
   &gt;  
    &lt;LinearLayout  
        android:orientation="horizontal"  
        android:gravity="center\_horizontal"  
        android:layout\_width="fill\_parent"  
        android:layout\_height="wrap\_content"  
        &gt;  
            &lt;Button  
                android:text="Text Only"  
                android:id="@+id/Button01"  
                android:layout\_width="100px"  
                android:layout\_height="wrap\_content"  
                /&gt;  
            &lt;Button  
                android:text="Icon Only"  
                android:id="@+id/Button02"  
                android:layout\_width="100px"  
                android:layout\_height="wrap\_content"  
                /&gt;  
            &lt;Button  
                android:text="Icon and Text"  
                android:id="@+id/Button03"  
                android:layout\_width="120px"  
                android:layout\_height="wrap\_content"  
                /&gt;  
    &lt;/LinearLayout&gt;  
&lt;/LinearLayout&gt;

然后在程序中为三个按钮分别创建点击事件，代码如下：

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38

[Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) btn;  
btn = ([Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky)) findViewById(R.id.Button01);  
btn.setOnClickListener(new [Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky).OnClickListener()  
{  
    public void onClick([View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) v)  
    {  
        Toast.makeText(getApplicationContext(), "Text toast test!", Toast.LENGTH\_LONG).show();  
    }  
});  
btn = ([Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky)) findViewById(R.id.Button02);  
btn.setOnClickListener(new [Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky).OnClickListener()  
{  
    public void onClick([View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) v)  
    {  
        Toast toast = new Toast(getApplicationContext());  
        ImageView view = new ImageView(getApplicationContext());  
        view.setImageResource(R.drawable.ic\_dialog\_alert);  
        toast.setView(view);  
        toast.show();  
    }  
});  
btn = ([Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky)) findViewById(R.id.Button03);  
btn.setOnClickListener(new [Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky).OnClickListener()  
{  
    public void onClick([View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) v)  
    {  
        Toast toast = Toast.makeText(getApplicationContext(), "Text and Icon test!", Toast.LENGTH\_LONG);  
        [View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) textView = toast.getView();  
        LinearLayout lay = new LinearLayout(getApplicationContext());  
        lay.setOrientation(LinearLayout.HORIZONTAL);  
        ImageView view = new ImageView(getApplicationContext());  
        view.setImageResource(R.drawable.ic\_dialog\_alert);  
        lay.addView(view);  
        lay.addView(textView);  
        toast.setView(lay);  
        toast.show();  
    }  
});

然后运行程序分别点击三个按钮可以看到三种不同情况下的Toast。  
[![android-toast-usage-02](http://www.androidx.org/wp-content/uploads/2009/10/android-toast-usage-02.png "android-toast-usage-02")](http://www.androidx.org/wp-content/uploads/2009/10/android-toast-usage-02.png)  
可以看到，最后一种图片加文字的显示显然是不符合我们的要求的，那么我们可以通过增加Toast的Layout来调整Toast上的布局方式。我们增加一个Toast的Layout描述xml文件，/res/layout/toast\_layout.xml：

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19

&lt;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"  
             &gt;  
    &lt;ImageView android:id="@+id/image"  
              android:layout\_width="wrap\_content"  
              android:layout\_height="fill\_parent"  
              android:layout\_marginRight="10dp"  
              /&gt;  
    &lt;TextView android:id="@+id/text"  
             android:layout\_width="wrap\_content"  
             android:layout\_height="fill\_parent"  
             android:textColor="#FFF"  
             /&gt;  
&lt;/LinearLayout&gt;

然后我们修改Button3的OnClick事件：

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17

btn = ([Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky)) findViewById(R.id.Button03);  
btn.setOnClickListener(new [Button](http://www.google.com/search?hl=en&q=allinurl%3Abutton%20java.sun.com&btnI=I%27m%20Feeling%20Lucky).OnClickListener()  
{  
    public void onClick([View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) v)  
    {  
        LayoutInflater inflater = getLayoutInflater();  
        [View](http://www.google.com/search?hl=en&q=allinurl%3Aview%20java.sun.com&btnI=I%27m%20Feeling%20Lucky) 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.ic\_dialog\_alert);  
        TextView text = (TextView) layout.findViewById(R.id.text);  
        text.setText("Hello! This is a custom toast!");  
        Toast toast = new Toast(getApplicationContext());  
        toast.setDuration(Toast.LENGTH\_LONG);  
        toast.setView(layout);  
        toast.show();  
    }  
});

再运行程序，显示如下：  
![android-toast-usage-03](http://www.androidx.org/wp-content/uploads/2009/10/android-toast-usage-03.png "android-toast-usage-03")

另外，我们还可以使用setGravity()方法来定位Toast在屏幕上的位置，例如toast.setGravity(Gravity.CENTER\_VERTICAL, 0, 0)可以把Toast定位在左上角。