以下来自android官方Toast使用说明的译文
toast是一种简单到弹出反馈操作。它只占用了消息所需要的空间大小,并在当前activity显示和互动。例如,当你退出正在编写email之前,会提示一个“草稿已保存”的toast来告知你可以稍后继续编辑。Toast会在一段时间后自动消失。
首先,通过Toast中的makeText()方法创建一个Toast对象。这个方法有三个参数:Context,消息文字,显示的时间长短。然后,通过show()让其显示出来:
[java]
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
标准的toast会出现在屏幕底部水平中间的位置上,你也可以调用setGravity(int,int,int)改变其显示的位置。这里的三个参数,分别表示:Gravity,x方向偏移,y方向偏移。
例如:如果你定义toast显示在左上角,那么,你可以这样写:
[java]
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果你想让其位置往右一些,你可以增加第二个参数的值,同理,向下调时,增加最后一个参数的值。
如果这样一个简单的消息不能让你满意,你还可以创建一个自己定义的布局。需要定义一个视图,可以是xml,也可以是代码的,然后调用setView(View)设置布局。
例如:
[html]
<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="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 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="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>
代码:
[java]
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
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();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
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)填充布局:第一个布局是layout的ID,第二个是根视图。你可以使用填充布局得到更多的内部视图,得到例如ImageView和TextView元素。最后,通过Toast(Context)创建一个对象,再设置显示位置、显示时间长短,然后调用setView(View)。现在,你可以调用show()让自定义的布局样式显示出来。
注意:不要使用公开的构造方法,除非你需要自定义视图。如果不使用特别布局的话,你应该使用makeText(Context,int,int)来创建Toast。