<Android 基础(十六)> Toast
介绍
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的更多相关文章
- Toast显示图文界面——Android开发之路1
Toast的多种使用方法 Toast其实是一个功能特别强大的组件,不仅仅可以吐司一个文本内容,还可以吐司图片以及图文混排的界面.具体用法如下: 第一种:简单的纯文本内容的吐司: Toast.makeT ...
- 【完全开源】知乎日报UWP版:增加Live磁贴、Badge、以及Toast通知
目录 说明 实现方法 APP生命期 后台任务 说明 之前网上有人建议增加磁贴(tile).徽章(badge)功能.利用周末的时间,将这两个功能添加上去了.如果将磁贴固定到开始屏幕,磁贴就会循环播放首页 ...
- Windows10自适应和交互式toast通知[1]
阅读目录: 概述 toast通知的结构 视觉区域(Visual) 行为(Actions) 特定场景下的Toast通知 带多内容的通知 带行为的通知(例子1) 带行为的通知(例子2) 带文本输入框和行为 ...
- 一个可随意定位置的带色Toast——开源代码Crouton的简单使用
今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定是自定义View呀,结果在浏览中发现还有这样的一个开源代码——Crouton. 几经折腾,发现这个东西 ...
- ios新手开发——toast提示和旋转图片加载框
不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...
- 安卓---Toast工具类,有点懒
package com.liunan.myfirstapp.util; import android.content.Context; import android.widget.Toast; /** ...
- Android 更改 Toast 的默认位置
Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适.比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没 ...
- toast组件小结
简介:toast是"吐司"的意思,它属于android杂项组件,是一个简单的消息提示框,类似于javascript中的alert. 作用 显示文本 显示图片 显示图文 3.常用方法 ...
- Toast 工具
public class Utils { private static Toast toast; public static void showtoast(Context context,String ...
- Android带图片的Toast(自定义Toast)
使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...
随机推荐
- Docker部署MySql应用
1. MySQL部署 1.1拉取MySQL镜像 docker pull mysql 查看镜像 docker images 1.2创建MySQL容器 docker run -di --name piny ...
- git相关问题处理
1.在git push时无法提交代码,相对于git服务器上,本身代码可能不是最新的,因此提交的时候会报以下这个错误 Updates were rejected because the tip of y ...
- Nexus 相关
https://help.sonatype.com/repomanager3/download https://www.jianshu.com/p/5fc8fb14d25c
- 【模板】缩点 tarjan+dp
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- virturalenv 虚拟环境
一.window系统 1.virtualenv的使用 2.pycharm使用 环境变量,path的作用:命令行中执行的命令,他们的路径,必须在path路径中,如果命令行找不到该命令,就是说path没写 ...
- springboot整合redis存放session
y进入maven依赖: <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springfra ...
- php 在 匿名函数中 调用自身。。
//php闭包实现函数的自调用,也就是实现递归 function closure($n,$counter,$max){ //匿名函数,这里函数的参数加&符号是,引址调用参数自己 $fn = f ...
- Codeforces Round #305 (Div. 2) A
Description While Mike was walking in the subway, all the stuff in his back-bag dropped on the groun ...
- IIS Express被局域网访问
在 文件夹 C:\Users\administrator\Documents\IISExpress\config 下面 applicationhost.config 文件里 找到相应的项目 如 < ...
- Lambda 快速改造
对于只有一个参数的接口类,可以使用e->{} idea会给出快速提示 先写正常的表达式 将虚线处直接删掉, 在原来的方法参数和左大括号之间加上 -> 改造后比原来少了几行, 对于熟手阅读 ...