一. 继承关系
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(Context context);
static Toast makeText(Context context, int resId, int duration);
static Toast makeText(Context context, CharSequence text, int duration)
void setText(int resId); void setText(CharSequence s);
void setView(View view);
void setDuration(int duration);
void setGravity(int gravity, int xOffset, int yOffset);
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); 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 = 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的用法总结(五种用法)