介绍

A toast provides simple feedback about an operation in a small popup. It only fills the amount of

space required for the message and the current activity remains visible and interactive. For

example, navigating away from an email before you send it triggers a “Draft saved” toast to let

you know that you can continue editing later. Toasts automatically disappear after a timeout.

翻译:

Toast以一种小弹框的方式来给予用户反馈,它只需要消息可以显示出来的那小部分空间,同时Activity依

然可见可交互。例如,当你写邮件的时候退出,会触发“草稿已保存”的Toast来让你知道你以后可以继续

编辑这封邮件。Toast会在一段时间后自己消失。


类结构

Toast的成员

成员解释

成员 含义
Toast(Context) 构造函数
show() 显示Toast
cancel() 取消Toast
setMargin(float,float) 设置横向的margin和纵向的margin
setGravity(int ,int, int) 设置toast的位置重心和相对的XOffset, YOffset
makeText(Context, CharSequence, int) 利用字符串创建Toast
makeText(Context, int, int) 利用字符串资源创建Toast
setView(View) 设置自定义Toast显示的View
setDuration(int) 设置Toast的显示时间
setText 设置Message文本内容
备注: 部分getter方法和setter方法对应,没有写入

实际使用

布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="mraz.com.toastdemo.MainActivity"><Button
        android:id="@+id/bt_simple"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorAccent"android:text="Simple Toast" /><Button
        android:id="@+id/bt_custom"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:background="@color/colorPrimary"android:text="Custom Toast" />
</LinearLayout>

自定义的Toast布局
custom_toast.xml

<?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="40dp"android:background="@drawable/shape_bg"android:orientation="horizontal"><ImageView
        android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/ic_account_circle_black_24dp" /><TextView
        android:layout_width="wrap_content"android:layout_height="40dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:gravity="center"android:text="This is a Custom Toast !"android:textStyle="italic" />
</LinearLayout>

代码内容

package mraz.com.toastdemo;import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Context mContext;Button btSimple, btCustom;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = this;btSimple = (Button) findViewById(R.id.bt_simple);btCustom = (Button) findViewById(R.id.bt_custom);btSimple.setOnClickListener(this);btCustom.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.bt_simple:Toast.makeText(mContext, " This is a simple toast", Toast.LENGTH_SHORT).show();break;case R.id.bt_custom:LayoutInflater layoutInflater = LayoutInflater.from(mContext);View custToast = layoutInflater.inflate(R.layout.custom_toast, null);Toast toast = new Toast(mContext);toast.setView(custToast);toast.setDuration(Toast.LENGTH_SHORT);toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);toast.show();break;}}
}

实际效果

备注

Toast的内容比较简单,但是开发过程中应该会经常用到,说不定偶尔还会碰到显示不出来的情况,不要忘记执行show()方法,当然,还有可能是其他的原因导致你的Toast无法显示~

转载于:https://www.cnblogs.com/lanzhi/p/6467188.html

Android 基础(十六) Toast相关推荐

  1. 妥妥的去面试之Android基础(六)

    笔者由于在近期需要找工作,所以近期最主要的任务就是准备面试,不打无准备之仗.只有你准备充分了,那么你想得到的机会才可能有机会入你怀中. 笔者会将准备面试的学习过程记录下来,方便自己复盘的同时也希望能给 ...

  2. Android 第十六课 使用LitePal查询数据

    LitePal在查询API方面做了非常多的优化,基本上可以满足绝大多数场景的查询需求,并且代码也十分整洁. 例如我们需要查询表中的所有数据: List<books> = DataSuppo ...

  3. Android 基础四五六之:四大组件详解

    Android 四大组件 android四大组件分别是:Activity, service,content provider,broadcast receiver 今天就带大家详细的学一波基本功,需要 ...

  4. Java必备基础十六——输入与输出流相关类详细介绍

    坚持的第16篇. 鸡汤一下:真正能让你走远的,都是自律.积极和勤奋 文章目录 序言 一.什么是流 二.流的种类划分 1.按数据流的方向分:输入流.输出流 1.1 定义 1.2 层次结构 2.按处理数据 ...

  5. Android提高十六篇之使用NDK把彩图转换灰度图

    在Android上使用JAVA实现彩图转换为灰度图,跟J2ME上的实现类似,不过遇到频繁地转换或者是大图转换时,就必须使用NDK来提高速度了.本文主要通过JAVA和NDK这两种方式来分别实现彩图转换为 ...

  6. 满足低调之心基础十六

    一些css基础内容补充

  7. Android 第十六章 HorizontalScrollView

    继承 java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ andr ...

  8. Android基础教程(六)之------- 参数的传递(Bundle)

    Activity间的简单数据传递 Bundle 记录如下: 1.建2个Activity 跳转 2: 第一个文件 Intent intent = new Intent();             in ...

  9. Mysql基础(十六):explain命令SQL执行计划

    目录 写在前面 1.通过explain命令得到的SQL执行计划(1) 2.通过explain命令得到的SQL执行计划(2) 3.通过explain命令得到的SQL执行计划(3) 4.通过explain ...

  10. Android开发笔记(八十六)几个特殊的类

    接口interface interface是一些功能的集合,但它只定义了对象必须实现的成员,而不包含成员的实现代码,成员的具体代码由实现接口的类提供.Android对接口的使用场景主要有三类:事件监听 ...

最新文章

  1. 2D-2D:对极约束
  2. 合并远程仓库到本地_Git工作区、暂存区、本地仓库和远程仓库科普
  3. 计算机课件制作技能,经济论文写作PPT制作有哪些技巧?
  4. java B2B2C源码电子商务平台 - Zuul回退机制
  5. DP——背包问题(一)
  6. LINQ之路 5:LINQ查询表达式
  7. 如何关掉Microsoft Office Click-to-Run服务
  8. mysql profile 调试sql_SQL Server profile使用技巧
  9. QQ和微信使用到的网络协议
  10. linux 卡在grub_浅析 GRUB 如何加载 Linux kernel
  11. cv::cornerSubPix()亚像素角点检测
  12. 【转】android gravity属性 和 weight属性
  13. D-Bus 性能分析
  14. MATLAB常见问题:小数保留有效数字位数相关问题/除法结果问题/数据显示格式设置
  15. Scratch1.4案例:射日小游戏
  16. 孤尽训练营打卡日记day17--面向对象
  17. kvm切换器linux换屏方法,kvm切换器连接图及安装方法
  18. Vue突然报错 doesn‘t work properly without JavaScript enabled
  19. FAQ | 为大屏幕设备构建应用的常见问题解答
  20. 计算机网络——第2章 物理层

热门文章

  1. Nature展示迄今为止最详细的“人脑零部件清单”
  2. 华为5G手机芯片被唱衰:美研究机构拆解6款量产机,不谈能力对标高通骁龙X50...
  3. 波士顿咨询:2018最具创新力企业50强
  4. 机器视觉行业的现状和未来
  5. 美媒:中关村取代硅谷获评全球最大科技中心
  6. 新基建来势汹汹,开发者如何捍卫其安全?
  7. 面试最后如何回答“你有什么要问我的吗?”
  8. 大写“惨”?三次改变世界、却被无情出局的程序员大牛!
  9. 漫画:为什么程序员喜欢使用 0 ≤ i 10 左闭右开形式写 for 循环?
  10. Android 读取本地图片