转载别人的写的不错,自己添加点注意事项,号称有5种效果。

1.默认效果

代码

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

2.自定义显示位置效果

代码

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

3.带图片效果

代码

  1. toast = Toast.makeText(getApplicationContext(),
  2. "带图片的Toast", Toast.LENGTH_LONG);
  3. toast.setGravity(Gravity.CENTER, 0, 0);
  4. LinearLayout toastView = (LinearLayout) toast.getView();
  5. ImageView imageCodeProject = new ImageView(getApplicationContext());
  6. imageCodeProject.setImageResource(R.drawable.icon);
  7. toastView.addView(imageCodeProject, 0);
  8. toast.show();

4.完全自定义效果

代码

  1. LayoutInflater inflater = getLayoutInflater();
  2. View layout = inflater.inflate(R.layout.custom,
  3. (ViewGroup) findViewById(R.id.llToast));
  4. ImageView image = (ImageView) layout
  5. .findViewById(R.id.tvImageToast);
  6. image.setImageResource(R.drawable.icon);
  7. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
  8. title.setText("Attention");
  9. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
  10. text.setText("完全自定义Toast");
  11. toast = new Toast(getApplicationContext());
  12. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  13. toast.setDuration(Toast.LENGTH_LONG);
  14. toast.setView(layout);
  15. toast.show();

5.其他线程

代码

  1. new Thread(new Runnable() {
  2. public void run() {
  3. showToast();
  4. }
  5. }).start();

完整代码

1.Main,java

  1. package com.wjq.toast;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.Gravity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.View.OnClickListener;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. public class Main extends Activity implements OnClickListener {
  15. Handler handler = new Handler();
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  21. findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
  22. this);
  23. findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  24. findViewById(R.id.btnCustomToast).setOnClickListener(this);
  25. findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
  26. }
  27. public void showToast() {
  28. handler.post(new Runnable() {
  29. @Override
  30. public void run() {
  31. Toast.makeText(getApplicationContext(), "我来自其他线程!",
  32. Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. }
  36. @Override
  37. public void onClick(View v) {
  38. Toast toast = null;
  39. switch (v.getId()) {
  40. case R.id.btnSimpleToast:
  41. Toast.makeText(getApplicationContext(), "默认Toast样式",
  42. Toast.LENGTH_SHORT).show();
  43. break;
  44. case R.id.btnSimpleToastWithCustomPosition:
  45. toast = Toast.makeText(getApplicationContext(),
  46. "自定义位置Toast", Toast.LENGTH_LONG);
  47. toast.setGravity(Gravity.CENTER, 0, 0);
  48. toast.show();
  49. break;
  50. case R.id.btnSimpleToastWithImage:
  51. toast = Toast.makeText(getApplicationContext(),
  52. "带图片的Toast", Toast.LENGTH_LONG);
  53. toast.setGravity(Gravity.CENTER, 0, 0);
  54. LinearLayout toastView = (LinearLayout) toast.getView();
  55. ImageView imageCodeProject = new ImageView(getApplicationContext());
  56. imageCodeProject.setImageResource(R.drawable.icon);
  57. toastView.addView(imageCodeProject, 0);
  58. toast.show();
  59. break;
  60. case R.id.btnCustomToast:
  61. LayoutInflater inflater = getLayoutInflater();
  62. View layout = inflater.inflate(R.layout.custom,
  63. (ViewGroup) findViewById(R.id.llToast));
  64. ImageView image = (ImageView) layout
  65. .findViewById(R.id.tvImageToast);
  66. image.setImageResource(R.drawable.icon);
  67. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
  68. title.setText("Attention");
  69. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
  70. text.setText("完全自定义Toast");
  71. toast = new Toast(getApplicationContext());
  72. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  73. toast.setDuration(Toast.LENGTH_LONG);
  74. toast.setView(layout);
  75. toast.show();
  76. break;
  77. case R.id.btnRunToastFromOtherThread:
  78. new Thread(new Runnable() {
  79. public void run() {
  80. showToast();
  81. }
  82. }).start();
  83. break;
  84. }
  85. }
  86. }

2.main,xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
  5. <Button android:layout_height="wrap_content"
  6. android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  7. android:text="默认"></Button>
  8. <Button android:layout_height="wrap_content"
  9. android:layout_width="fill_parent" android:text="自定义显示位置"
  10. android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
  11. <Button android:layout_height="wrap_content"
  12. android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  13. android:text="带图片"></Button>
  14. <Button android:layout_height="wrap_content"
  15. android:layout_width="fill_parent" android:text="完全自定义"
  16. android:id="@+id/btnCustomToast"></Button>
  17. <Button android:layout_height="wrap_content"
  18. android:layout_width="fill_parent" android:text="其他线程"
  19. android:id="@+id/btnRunToastFromOtherThread"></Button>
  20. </LinearLayout>

3.custom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_height="wrap_content" android:layout_width="wrap_content"
  5. android:background="#ffffffff" android:orientation="vertical"
  6. android:id="@+id/llToast" >
  7. <TextView
  8. android:layout_height="wrap_content"
  9. android:layout_margin="1dip"
  10. android:textColor="#ffffffff"
  11. android:layout_width="fill_parent"
  12. android:gravity="center"
  13. android:background="#bb000000"
  14. android:id="@+id/tvTitleToast" />
  15. <LinearLayout
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical"
  18. android:id="@+id/llToastContent"
  19. android:layout_marginLeft="1dip"
  20. android:layout_marginRight="1dip"
  21. android:layout_marginBottom="1dip"
  22. android:layout_width="wrap_content"
  23. android:padding="15dip"
  24. android:background="#44000000" >
  25. <ImageView
  26. android:layout_height="wrap_content"
  27. android:layout_gravity="center"
  28. android:layout_width="wrap_content"
  29. android:id="@+id/tvImageToast" />
  30. <TextView
  31. android:layout_height="wrap_content"
  32. android:paddingRight="10dip"
  33. android:paddingLeft="10dip"
  34. android:layout_width="wrap_content"
  35. android:gravity="center"
  36. android:textColor="#ff000000"
  37. android:id="@+id/tvTextToast" />
  38. </LinearLayout>
  39. </LinearLayout>

注意: 使用toast = new Toast(context); 一定要先 toast.setView() ,然后才能toast.Show() 不然会出错。同时,我使用

LinearLayout toastView = (LinearLayout) toast.getView(); 然后再在这个LinearLayout种添加TextView会出错,后来就直接定义一个LinearLayout了。

Android Toast 总结相关推荐

  1. [Android] Toast问题深度剖析(二)

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者: QQ音乐技术团队 题记 Toast 作为 Android 系统中最常用的类之一,由于其方便的api设计和简洁的交互体验,被我们所广泛采用 ...

  2. android toast的使用

    今天,没有什么可以写的,在网上查找资料,发现toast有很多知识点,所以记录一下. Toast Toast是为了给当前视图显示一个浮动的显示块,它永远不会获得焦点.一般用于提示一些不那么引人注目,但是 ...

  3. 使用Kotlin的Android Toast

    Android Toast is a UI widget that pops up on the screen for some time. It's very similar to a notifi ...

  4. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  5. Android Toast cancel和show深入浅出实战

    说到Android Toast,几乎都很熟悉吧,下面讲讲怎么实现下面几种场景: 1.连续点击一个按钮,每次都产生一个新的Toast并且调用show方法 问题:触发了toast以后,toast内容会一直 ...

  6. 自定义 Android toast 字体大小

    让Android Toast 居中 可以设置 字体大小 Toast toast = Toast.makeText(this, "这是一个简单的自定义Toast", Toast.LE ...

  7. Android Toast 自定义显示时长

    Android Toast 只支持两种时间 LENGTH_SHORT 2 秒,LENGTH_LONG 3.5 秒,但是有场景需要自定义显示时长就会有问题,所以需要自定义实现,下边是自定义的类,通过定时 ...

  8. robotframework + appium 获取android toast

    android toast 获取主要方式是在出现toast的时候查找元素:xpath=//*[contains(@text,'记同步')]  ,该xpath 表示为toast信息含有  "记 ...

  9. qt自定义控件-模拟Android toast提示窗口

    一.前言 好久没写博客了,最近一直写材料,很难受,在家做点小东西,正好遇到了想做的效果,在桌面程序实现Android的toast效果 二.环境 目标机linux,测试机window10 qt5.7 本 ...

  10. Android Toast类

    1. 默认创建 Toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据用户设置的显示时间后自动消失. Toast静态方法makeText(),生成Toast实例,并调用show()方 ...

最新文章

  1. Real-Time DNA Sequencing from Single Polymerase Molecules
  2. spring3.0设置定时任务
  3. 【进阶技巧】如何绘制高颜值XMind思维导图?色彩使用很重要!
  4. hive 行转列和列转行的方法_Hive超详细存储
  5. mysql中的类型与java_mysql与java数据类型对应关系
  6. Linux系统开机启动流程介绍
  7. cf1009F. Dominant Indices
  8. SVN更新时报403错误
  9. 80.简单搭建nodeJS服务,访问本地站点文件
  10. 打造极致Material Design动画风格Button
  11. python字符串转为ascii码_Python学习经验:无重复字符的最长子串
  12. java se下载完怎么启动_【Java SE】如何安装JDK以及配置Java运行环境
  13. python程序员面试宝典 剑指offer_程序员面试宝典+剑指Offer + 算法100题系列 + 15个经典算法下载...
  14. 端口扫描工具Namp
  15. 通信系统中语音信号的仿真分析
  16. 单片机AD采样的几种算法
  17. 计算机科学与技术本科知识体系
  18. react实现markdown编辑器
  19. css3复习知识点概括1(根据W3S顺序)
  20. 计算机专业应届毕业生找工作一定要知道的面试题--必背版

热门文章

  1. 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
  2. 总结一下用caffe跑图片数据的研究流程接上篇
  3. 数字的格式化c语言课程设计,【图片】发几个C语言课程设计源代码(恭喜自己当上技术小吧主)【东华理工大学吧】_百度贴吧...
  4. 关于webpack升级过后不能打包的问题;
  5. KnockoutJS 3.X API 第七章 其他技术(3) 延迟更新
  6. IE下图片切换的时候,图片总是切换不成功---根本问题是IE缓存图片
  7. 将帐套升级到百万用户纪念版实践教程
  8. Compiz Check测试Linux桌面3D兼容性
  9. MOSS 2010:Visual Studio 2010开发体验(13)——列表开发之列表实例
  10. MPI集群安装、MPI安装