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

1.默认效果

代码

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

2.自定义显示位置效果

代码

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

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();

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();

5.其他线程

代码

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

custom.xml

<?xml version="1.0" encoding="utf-8"?> 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="#ffffffff"
    android:orientation="vertical"
    android:id="@+id/llToast" >
    <TextView
      android:layout_height="wrap_content"
      android:layout_margin="1dip"
      android:textColor="#ffffffff"
      android:layout_width="fill_parent"
      android:gravity="center"
      android:background="#bb000000"
      android:id="@+id/tvTitleToast" />
    <LinearLayout
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:id="@+id/llToastContent"
      android:layout_marginLeft="1dip"
      android:layout_marginRight="1dip"
      android:layout_marginBottom="1dip"
      android:layout_width="wrap_content"
      android:padding="15dip"
      android:background="#44000000" >
      <ImageView
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:id="@+id/tvImageToast" />
      <TextView
        android:layout_height="wrap_content"
        android:paddingRight="10dip"
        android:paddingLeft="10dip"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:textColor="#ff000000"
        android:id="@+id/tvTextToast" />
    </LinearLayout>
  </LinearLayout>

Android Toast.makeText用法的更多相关文章

  1. Android——Toast.makeText()

    1.Toast 作用 它是android中一种简单的消息提示框,它没有焦点(即不能被点击),会根据设置的时间自动消失. 2.介绍下Toast的参数 Toast t = Toast.makeText(M ...

  2. android里Toast的用法

    在活动中,可以通过findViewById()方法获取到在布局文件中定义的元素,这里我们传入R.id.button_1,来得到按钮的实例,这个值是刚才在first_layout.xml中通过andro ...

  3. Android中Toast的用法简介

    转自:http://www.cnblogs.com/GnagWang/archive/2010/11/26/1888762.html Toast是Android中用来显示显示信息的一种机制,和Dial ...

  4. Android Toast 总结(五种用法)

    Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...

  5. 消息模式Toast.makeText的几种常见用法

    Toast 是一个 View 视图,快速的为用户显示少量的信息. Toast 在应用程序上浮动显示信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于 一些帮助 / 提示. Toast 最 ...

  6. 4、android BroadcastReceiver详细用法

    BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...

  7. [安卓] 4、CheckBox、RadioButton和Toast简单用法

      和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...

  8. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  9. 【转】通知 Toast详细用法(显示view)

    原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...

随机推荐

  1. 概率检索模型:BIM+BM25+BM25F

    1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...

  2. 【Redis学习之二】Redis:redis.conf 配置详解

    参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程  daemonize no2. 当Redis以守护进程方式运行 ...

  3. linux常用命令:route 命令

    Linux系统的route 命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需 要一台连接两个网络的路由器 ...

  4. linux常用命令:du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项] [文件|目录] 2. ...

  5. SQL语句常见优化方法

    Sql优化方法 先进行选择运算(where limit)再进行连接运算 where子句中应把过滤性最强的条件放在最前面 where子句中字段的顺序应和组合索引中字段顺序一致 使用索引 使用覆盖索引来避 ...

  6. linux服务器文件索引inodes满了

    inode节点中,记录了文件的类型.大小.权限.所有者.文件连接的数目.创建时间与更新时间等重要的信息,还有一个比较重要的内容就是指向数据块的指针.一般情况不需要特殊配置,如果存放文件很多,需要配置. ...

  7. 如何使用Unity制作虚拟导览(一)

    https://www.cnblogs.com/yangyisen/p/5108289.html Unity用来制作游戏已经是目前市场上的一个发展趋势,而且有越来越多的公司与开发者不断的加入,那么Un ...

  8. nginx作防盗链设置

    盗链是一种损害原有网站合法权益,给原网站所在服务器造成额外负担的非法行为. 盗链的实现原理: 客户端向服务器请求资源时,为了减少网络带宽,提高响应时间,服务器一般不会一次将所有资源完整地传回给客户端. ...

  9. 在python3下使用OpenCV 显示图像

    在Python3下用使用OpenCV比在C,C++里开发不止快捷一点点, 原型开发的时候蛮有用. 这里用的OpenCV 加载图片, 用的imshow画图 # -*- coding: utf-8 -*- ...

  10. P4052 [JSOI2007]文本生成器

    P4052 [JSOI2007]文本生成器 AC自动机+dp 优秀题解传送门 设f[ i ][ j ]表示串的长度为 i ,当前在 j 点时不可识别的串的方案数 最后用总方案数减去不可识别方案数就是答 ...