Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示该信息。

该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。有两个例子就是音量控制和设置信息保存成功。

使用该类最简单的方法就是调用一个静态方法,让他来构造你需要的一切并返回一个新的 Toast 对象。

1、我们首先来看看Toast常用 的默认效果:

2、我们还可以自定义位置:

3、带图片的:

4、完全实现我们自己的自定义效果:

5、可以由其它线程更新:

查看源代码:

HelloToastActivity.java

[java] view plaincopyprint?
  1. package hb.android.hellotoast;
  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.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. public class HelloToastActivity extends Activity {
  15. /** Called when the activity is first created. */
  16. Button btn_default;
  17. Button btn_define;
  18. Button btn_all_define;
  19. Button btn_image_define;
  20. Button btn_other_thread;
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. initButton();
  26. btn_all_define.setOnClickListener(new MyOnClickListerer());
  27. btn_define.setOnClickListener(new MyOnClickListerer());
  28. btn_other_thread.setOnClickListener(new MyOnClickListerer());
  29. btn_image_define.setOnClickListener(new MyOnClickListerer());
  30. btn_default.setOnClickListener(new MyOnClickListerer());
  31. }
  32. public void initButton() {
  33. btn_all_define = (Button) findViewById(R.id.btn_all_define);
  34. btn_default = (Button) findViewById(R.id.btn_default);
  35. btn_define = (Button) findViewById(R.id.btn_define);
  36. btn_image_define = (Button) findViewById(R.id.btn_image_define);
  37. btn_other_thread = (Button) findViewById(R.id.btn_other_thread);
  38. }
  39. private class MyOnClickListerer implements OnClickListener {
  40. Handler handler = new Handler();
  41. @Override
  42. public void onClick(View v) {
  43. if (v == btn_default) {
  44. Toast.makeText(getApplicationContext(), "这 是默认效果",
  45. Toast.LENGTH_SHORT).show();
  46. } else if (v == btn_define) {
  47. Toast toast = Toast.makeText(getApplicationContext(),
  48. "这是自定义位置", Toast.LENGTH_SHORT);
  49. toast.setGravity(Gravity.CENTER, 0, 0);
  50. toast.show();
  51. } else if (v == btn_image_define) {
  52. Toast toast = Toast.makeText(getApplicationContext(), "这是带图片的",
  53. Toast.LENGTH_SHORT);
  54. LinearLayout toastView = (LinearLayout) toast.getView();
  55. ImageView imageCodeProject = new ImageView(
  56. getApplicationContext());
  57. imageCodeProject.setImageResource(R.drawable.ic_launcher);
  58. toastView.addView(imageCodeProject, 0);
  59. toast.show();
  60. } else if (v == btn_all_define) {
  61. LayoutInflater inflater = getLayoutInflater();
  62. View view = inflater.inflate(R.layout.custom, null);
  63. ImageView iv = (ImageView) view.findViewById(R.id.tvImageToast);
  64. iv.setImageResource(R.drawable.ic_launcher);
  65. TextView title = (TextView) view
  66. .findViewById(R.id.tvTitleToast);
  67. title.setText("Attention");
  68. TextView text = (TextView) view.findViewById(R.id.tvTextToast);
  69. text.setText("完全自定义Toast");
  70. Toast toast = new Toast(getApplicationContext());
  71. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  72. toast.setDuration(Toast.LENGTH_LONG);
  73. toast.setView(view);
  74. toast.show();
  75. } else if (v == btn_other_thread) {
  76. new Thread(new Runnable() {
  77. public void run() {
  78. System.out.println("d");
  79. showToast();
  80. }
  81. }).start();
  82. }
  83. }
  84. public void showToast() {
  85. handler.post(new Runnable() {
  86. @Override
  87. public void run() {
  88. Toast.makeText(getApplicationContext(), "我来自其他线程!",
  89. Toast.LENGTH_SHORT).show();
  90. }
  91. });
  92. }
  93. }
  94. }

custom.xml

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

main.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:gravity="center_horizontal">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="建立属于你自己的Toast" />
  11. <Button
  12. android:id="@+id/btn_default"
  13. android:text="默认"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:gravity="center_horizontal"/>
  17. <Button
  18. android:id="@+id/btn_define"
  19. android:text="自定义位置"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:gravity="center_horizontal"/>
  23. <Button
  24. android:id="@+id/btn_image_define"
  25. android:text="带图片"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:gravity="center_horizontal"/>
  29. <Button
  30. android:id="@+id/btn_all_define"
  31. android:text="完全自定义效果"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:gravity="center_horizontal"/>
  35. <Button
  36. android:id="@+id/btn_other_thread"
  37. android:text="来自其它纯种"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:gravity="center_horizontal"/>
  41. </LinearLayout>

源码下载:Android  Toast用法详解(各种自定义Toast)

ref:http://blog.csdn.net/huangbiao86/article/details/6965669

android Toast五种特效相关推荐

  1. java简述常见的布局极其特点_请简要说明 Android 中五种常见布局的特点。_学小易找答案...

    [简答题]请简要说明有序广播和无序广播的区别 [简答题]请简要说明 Android 程序结构中 AndroidManifest.xml 的功能? [简答题]简述李村站人工办理进路的作业过程. [简答题 ...

  2. Android中五种常用对话框的使用

    场景 Android中常用的五种对话框为 常规对话框.带列表的对话框.自定义的对话框.带进度条的对话框.带日期选择器的对话框. 注: 博客: https://blog.csdn.net/badao_l ...

  3. android toast几种使用方法 (转)

    toast经常会用到,今天做个总结,特别是自定义toast的布局,值得一看. 一.默认展示 // 第一个参数:当前的上下文环境.可用getApplicationContext()或this // 第二 ...

  4. 【android】五种控制Android应用的权限的方法

    转自:http://www.cnbeta.com/articles/181913.htm 1  为什么Android总是事无巨细地告诉你应用索取的每一项权限? 相比Apple,Microsoft严格控 ...

  5. Android中五种常用的menu

    Android Menu在手机的应用中起着导航的作用,作者总结了5种常用的Menu. 1.左右推出的Menu 前段时间比较流行,我最早是在海豚浏览器中看到的,当时耳目一新.最早使用左右推出菜单的,听说 ...

  6. Android UI开发第二十九篇——Android中五种常用的menu(菜单)

    Android Menu在手机的应用中起着导航的作用,作者总结了5种常用的Menu. 1.左右推出的Menu 前段时间比较流行,我最早是在海豚浏览器中看到的,当时耳目一新.最早使用左右推出菜单的,听说 ...

  7. [Android Studio]掌握Android Studio的五种常见控件和五种常见布局

    目录 一.View和ViewGroup 二.Android的五种常见控件 2.1 文本控件 2.1.1 TextView 2.1.2 EditText 2.2 按钮控件 2.2.1 Button 2. ...

  8. Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout

    目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...

  9. Android特效 五种Toast详解

    Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.而且Toast主要用于向用户显示提示消 ...

  10. Android数据存储五种方式总结

    1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用ContentProvider存储数据 5 网络存储数据 下面详细讲解 ...

最新文章

  1. ASP.NET4.0中客户端ID的生成
  2. 量子计算机编程原理简介 和 机器学习
  3. Apache URL重写的配置 及其 apache500错误
  4. 接入腾讯云短信服务(史上最详细+该短信服务如何申请成功+发送短信验证码API讲解+相关错误分析)
  5. 如何 给给软件开发 添加 代理_如何与软件开发公司有效沟通
  6. 雪花飞舞的java程序_【图片】请问大神帮我看看一段代码,老是提示空指针异常【java吧】_百度贴吧...
  7. genymotion集成eclipse插件安装教程
  8. .Net 1.1 到 .Net 2.0 开发日志
  9. java教程配置通达信,通达信公式转换JAVA,通达信20个经典公式
  10. 自创RTSP 服务器 用多款客户端软件测试接入可以,唯独VLC接入不了
  11. java ffmpeg amr mp3_java利用ffmpeg将amr、caf转mp3格式
  12. 给宝宝做一个cocos免费游戏-故事和开始界面
  13. WebView截取长图
  14. 反复踩坑的ceres安装-----ubuntu18.04
  15. office 无法正常读取打开 pptx 文件的解决办法
  16. Unresolved compilation problem,问题
  17. 华为设备配置BGP负载分担
  18. 1.0、Python概述
  19. 谈谈卷积神经网络和循环神经网络
  20. CodeM2018 初赛A轮 第1,2题

热门文章

  1. Java反射机制demo(三)—获取类中的构造函数
  2. 【转】js字符串转换成数字
  3. 对WinForm的App.config文件进行加密
  4. linux 下解决arp病毒攻击时上网问题的最简单的治标办法
  5. xPath(他山之石)
  6. (Sublime Text 3)完美替换 GAMS 难用的编辑器
  7. 图片报错,显示默认图片
  8. jQuery中文文档
  9. Oracle 数字与空值的排序问题
  10. ACL-IJCNLP 2021|行业首个少样本NER数据集,清华联合阿里达摩院开发