介绍

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; @Override
protected 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); } @Override
public 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无法显示~

<Android 基础(十六)> Toast的更多相关文章

  1. Toast显示图文界面——Android开发之路1

    Toast的多种使用方法 Toast其实是一个功能特别强大的组件,不仅仅可以吐司一个文本内容,还可以吐司图片以及图文混排的界面.具体用法如下: 第一种:简单的纯文本内容的吐司: Toast.makeT ...

  2. 【完全开源】知乎日报UWP版:增加Live磁贴、Badge、以及Toast通知

    目录 说明 实现方法 APP生命期 后台任务 说明 之前网上有人建议增加磁贴(tile).徽章(badge)功能.利用周末的时间,将这两个功能添加上去了.如果将磁贴固定到开始屏幕,磁贴就会循环播放首页 ...

  3. Windows10自适应和交互式toast通知[1]

    阅读目录: 概述 toast通知的结构 视觉区域(Visual) 行为(Actions) 特定场景下的Toast通知 带多内容的通知 带行为的通知(例子1) 带行为的通知(例子2) 带文本输入框和行为 ...

  4. 一个可随意定位置的带色Toast——开源代码Crouton的简单使用

    今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定是自定义View呀,结果在浏览中发现还有这样的一个开源代码——Crouton. 几经折腾,发现这个东西 ...

  5. ios新手开发——toast提示和旋转图片加载框

    不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...

  6. 安卓---Toast工具类,有点懒

    package com.liunan.myfirstapp.util; import android.content.Context; import android.widget.Toast; /** ...

  7. Android 更改 Toast 的默认位置

    Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适.比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没 ...

  8. toast组件小结

    简介:toast是"吐司"的意思,它属于android杂项组件,是一个简单的消息提示框,类似于javascript中的alert. 作用 显示文本 显示图片 显示图文 3.常用方法 ...

  9. Toast 工具

    public class Utils { private static Toast toast; public static void showtoast(Context context,String ...

  10. Android带图片的Toast(自定义Toast)

    使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...

随机推荐

  1. Qt 学习之路 2(22):事件总结

    Qt 学习之路 2(22):事件总结 豆子 2012年10月16日 Qt 学习之路 2 47条评论 Qt 的事件是整个 Qt 框架的核心机制之一,也比较复杂.说它复杂,更多是因为它涉及到的函数众多,而 ...

  2. How can I use wget in Windows

    http://www.ehow.com/how_10054131_use-wget-windows.html

  3. js 返回 差 集

    //接受两个数组,返回差集 function getDiffSet(a,b){ a.sort(charSort); b.sort(charSort); if(a.length>=b.length ...

  4. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  5. web服务器Nginx环境下如何实现安全证书https的配置

    https跟http的关系 https没出现之前,我们网站大多数都是http开头,http全名超文本传输协议,客户端据此获取服务器上的超文本内容.超文本内容则以HTML为主,客户端拿到HTML内容后可 ...

  6. Vi编辑器和Vim编辑器的区别及联系

    Vi和Vim它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面,vi使用于文本编辑,但是vim更适用于coding.vim的这些优势主要体现在 ...

  7. C++二维数组的动态声明

    int **a  =  new int* [m]   //分配一个指针数组,将其首地址保存在a中   . for(int i = 0; i < m; i++)   //为指针数组的每个元素分配一 ...

  8. 利用vue-cli搭建vue项目

    手把手教你用vue-cli搭建vue项目 本篇主要是利用vue-cli来搭建vue项目,其中前提是node和npm已经安装好,文章结尾将会简单提到一个简单的例子.使用vue-cli搭建项目最开始我也是 ...

  9. IDEA修改git账号及密码的方法

    IDEA修改git账号及密码的方法: 1.file->settings->passwords 2.重启IDEA 3.执行一次提交或更新 当执行提交或更新之后,idea会自动提示输入账号.密 ...

  10. JAVA中的for循环

    在Java程序中,要“逐一处理”――或者说,“遍历”――某一个数组或Collection中的元素的时候,一般会使用一个for循环来实现(当 然,用其它种类的循环也不是不可以,只是不知道是因为for这个 ...