1. 贴一段Android API-Toast

Toast

public class Toast 
extends Object

java.lang.Object

android.widget.Toast

A toast is a view containing a quick little message for the user. The
toast class helps you create and show those.

When the view is shown to the user, appears as a floating view over the
application. It will never receive focus. The user will probably be in the middle
of typing something else. The idea is to be as unobtrusive as possible, while
still showing the user the information you want them to see. Two examples are
the volume control, and the brief message saying that your settings have been
saved.

The easiest way to use this class is to call one of the static methods
that constructs everything you need and returns a new Toast object.

Constants

int  LENGTH_LONG

Show the view or text notification for a long period of
time.

int  LENGTH_SHORT

Show the view or text notification for a short period
of time.

Public constructors

Toast(Context context)

Construct an empty Toast object.

 

Public methods

void cancel()

Close the view if it's showing, or don't show it if it
isn't showing yet.

int getDuration()

Return the duration.

int getGravity()

Get the location at which the notification should
appear on the screen.

float getHorizontalMargin()

Return the horizontal margin.

float getVerticalMargin()

Return the vertical margin.

View getView()

Return the view.

int  getXOffset()

Return the X offset in pixels to apply to the gravity's
location.

int  getYOffset()

Return the Y offset in pixels to apply to the gravity's
location.

staticToast makeText(Context context, int resId, int
duration)

Make a standard toast that just contains a text view
with the text from a resource.

staticToast makeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

void setDuration(int duration)

Set how long to show the view for.

void setGravity(int gravity, int xOffset, int
yOffset)

Set the location at which the notification should
appear on the screen.

void setMargin(float horizontalMargin, float
verticalMargin)

Set the margins of the view.

void setText(int resId)

Update the text in a Toast that was previously created
using one of the makeText() methods.

void setText(CharSequence s)

Update the text in a Toast that was previously created
using one of the makeText() methods.

void setView(View view)

Set the view to show.

void show()

Show the view for the specified duration.

2.中文简介及用法举例

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。

2.1 默认效果

代码

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

2.2 自定义显示位置效果

代码

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

2.3 带图片效果

代码

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

2.4 完全自定义效果

代码

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

2.5 其他线程

代码

new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();

Android学习笔记之Toast详解的更多相关文章

  1. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  2. Android学习笔记_21_ViewFlipper使用详解 手势识别器

    一.介绍ViewFilpper类 1.1 屏幕切换 屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面:一个个性化设置 ...

  3. Android学习笔记4——Activity详解

    在 Android 开发过程中,与程序员打交道最多的应该就是作为四大组件之一的 Activity 了.接下来我们就一起来揭开 Activity 的神秘面纱吧~ 一.概述 什么是 Activity(活动 ...

  4. expect学习笔记及实例详解【转】

    1. expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示:1.1 首行加上/usr/bin/expect1.2 spawn: 后面加上需要执行的shell命令,比如说sp ...

  5. Android特效 五种Toast详解

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

  6. Docker技术入门与实战 第二版-学习笔记-3-Dockerfile 指令详解

    前面已经讲解了FROM.RUN指令,还提及了COPY.ADD,接下来学习其他的指令 5.Dockerfile 指令详解 1> COPY 复制文件 格式: COPY  <源路径> .. ...

  7. Redis学习笔记4-Redis配置详解

    在Redis中直接启动redis-server服务时, 采用的是默认的配置文件.采用redis-server   xxx.conf 这样的方式可以按照指定的配置文件来运行Redis服务.按照本Redi ...

  8. Struts2学习笔记(二)——配置详解

    1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...

  9. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

随机推荐

  1. LeetCode——Longest Repeating Character Replacement

    1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...

  2. LeetCode——Unique Binary Search Trees

    Question Given n, how many structurally unique BST's (binary search trees) that store values 1...n? ...

  3. 4.scala中的类

    版权申明:转载请注明出处.文章来源:http://bigdataer.net/?p=269 排版乱?请移步原文获得更好的阅读体验 1.针对不同字段生成的方法 字段生成的方法备注 var/val nam ...

  4. 贯众云平台脚本编写之判断、循环以及shell命令的使用

    最近使用贯众云平台工具写脚本,进行Ui界面的自动化测试.刚开始接触,确实碰到不少的问题,稍微难点的就是判断语句,循环语句以及shell命令的使用,尤其是对于咱们测试这样比较少接触代码的人来说. 其实吧 ...

  5. ELK 6.x 部署

    Elasticsearch版本:6.3.2 Kibana版本:6.3.2 1.es安装 按照官方提示操作即可. 通过yum安装或者下载tar包解压. 安装完成之后,需要修改一些配置 ①修改文件 /et ...

  6. Sorl初始

    Sorl是什么 ? Solr是Apache下的一个开源项目,使用Java基于Lucene开发的全文检索服务:是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可 ...

  7. Servlet与线程安全

    先说结论:servlet不是线程安全的. servlet运行过程 Servlet程序是由WEB服务器调用,web服务器收到客户端的Servlet访问请求后: ①Web服务器首先检查是否已经装载并创建了 ...

  8. IntelliJ IDEA 2017 主题安装及配置

    主题哪里下的? 网站:http://www.riaway.com/ 主题怎么安装? 然后选择文件,找到下载的主题,按步骤,重启之后即可使用: 主题如何配置? 以下所有配置基于 IntelliJ IDE ...

  9. matlab C++ (VS Qt)混合编程 / mxArray / QT5中文乱码

    一.混合编程环境搭建---依据我的情况,分成了4个部分: 1:破解matlab,因为matlab破解不完全,编译器不能使用,会出错.(参考https://blog.csdn.net/a12593012 ...

  10. IE8下使用asp.net core mvc+jquery ajaxSubmit问题

    由于项目中一些特殊的地方使用了ajaxSubmit提交数据,但发现在IE8中出现问题,使用该方式提交数据后,无法返回提交结果,而是直接下载该方法名的一个文件,翻阅了园子,终于找到了最简单的解决办法,特 ...