一. 继承关系

1
2
java.lang.Object
↳android.widget.Toast

Toast 是一个视图,其中包含给用户的简单提示。当 Toast 显示给用户时,在应用程序上显示为浮动视图,它永远不会受到关注,用户可能正在输入其内容。Toast 是想尽可能不打扰用户,同时仍向用户显示你希望他们看到的信息。

两个示例是音量控制以及一条简短的信息提示你的设置已经保存。

二. Toast 的一些方法

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
//构造一个空的Toast
Toast(Context context);
//返回一个仅包含带有资源文本(R.string....)的文本视图的标准吐司。
static Toast makeText(Context context, int resId, int duration);
//返回一个仅包含文本视图的标准吐司。
static Toast makeText(Context context, CharSequence text, int duration)

//设置Toast中的内容
void setText(int resId);
void setText(CharSequence s);

//设置显示的视图
void setView(View view);

//设置显示视图的时间。
//Toast.LENGTH_LONG //长时间显示视图或文本通知。
//Toast.LENGTH_SHORT //在短时间内显示视图或文本通知。
void setDuration(int duration);

//设置通知应在屏幕上出现的位置。
void setGravity(int gravity, int xOffset, int yOffset);

//显示Toast
void show();

//如果正在显示,请关闭视图;如果尚未显示,则不显示。
void cancel();

三. 实例

1. 默认的效果

1
Toast.makeText(this, "默认样式", Toast.LENGTH_SHORT).show();

AndroidStudio 默认有一个模板,打出 Toast 就可以快速创建 Toast 的代码。

2. 自定义位置

1
2
3
4
Toast toast = Toast.makeText(this, "自定义位置", Toast.LENGTH_SHORT);
//设置位置
toast.setGravity(Gravity.CENTER,0,0);
toast.show();

3. 带图片的

1
2
3
4
5
6
7
Toast toast = Toast.makeText(this, "带图片的", Toast.LENGTH_SHORT);
//设置图片
LinearLayout linearLayout = (LinearLayout) toast.getView();
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView,0);//第二个参数index可以决定和文本的相对位置
toast.show();

4. 完全自定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//加载布局
LinearLayout inflate = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.toast_layout, null);
//找到控件
TextView title = inflate.findViewById(R.id.title_text);
ImageView show = inflate.findViewById(R.id.show_img);
TextView main = inflate.findViewById(R.id.main_text);
//设置数据
title.setText("这是标题");
show.setImageResource(R.drawable.ic_launcher_background);
main.setText("这是主要内容");
//创建Toast
Toast toast = new Toast(this);
//设置位置
toast.setGravity(Gravity.RIGHT| Gravity.BOTTOM,12,40);
//设置持续时间
toast.setDuration(Toast.LENGTH_LONG);
//添加
toast.setView(inflate);
//显示
toast.show();

单独的显示图片可以使用

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
<!--布局文件-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

<ImageView
android:layout_marginTop="10dp"
android:id="@+id/show_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

<TextView
android:layout_marginTop="10dp"
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

</LinearLayout>

5. 其他线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Handler handler = new Handler();

//其他线程
new Thread(new Runnable() {
@Override
public void run() {
showToast();
}
}).start();

//显示其他线程
private void showToast() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "来自其他线程", Toast.LENGTH_SHORT).show();
}
});
}

参考文章

Android Toast的用法总结(五种用法)