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. linuxbash 父进程 子进程

    linux登陆linux,就获得一个bash,之后你的bash就是一个独立的进程,被称为pid的就是,之后你在bash下面执行的任何命令都是由这个bash所衍生的,那些被执行的命令被称为子进程.子进程 ...

  2. rpm服务的独立服务管理

    /etc/init.d  启动脚本的位置 /etc/sysconfig/ 初始化环境配置文件 /etc/   配置文件位置 /etc/xinetd.conf  xinetd配置文件 /etc/xine ...

  3. 结合ajax 的表单验证

    浪费了我两天的时间 我也是醉了 html  结构 <!-- 密码修改 --> <div class="modal fade" id="operatePa ...

  4. 论文笔记:2018 PRCV 顶会顶刊墙展

    Global Gated Mixture of Second-order Pooling for Imporving Deep Convolutional Neural Network(2018 NI ...

  5. PDF常见问题总结

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  6. linux常用命令(替换)

    1. vi 模式下的替换命令: s 表示替换(substitute),g表示全局搜索(global search) :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vi ...

  7. appium— Android定位webView里面的UI元素

    Android SDK中的UIAutomator中本身是不支持网页中的UI元素定位,下面介绍几种常用的定位app内部的网页的UI元素的方法. 一.使用chrome浏览器调试移动端网页 这是使用最多的一 ...

  8. 2018 Java线程热门面试题,你知道多少?

    面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会这样问?他 ...

  9. P2158/bzoj2190 [SDOI2008]仪仗队

    P2158 [SDOI2008]仪仗队 欧拉函数 计算下三角的点数再*2+1 观察斜率,自行体会 #include<iostream> #include<cstdio> #in ...

  10. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...