一般我们提示的时候都是直接提示文字的,其实Toast也可以显示图片

常用方法

  1. Toast.makeText(context,text,duration)这返回一个Toast对象
  2. toast.setDureation(duration)设置持续时间
  3. toast.setGravity(gravity,xOffest,yOffset)设置Toast的位置
  4. toast.setText(s);设置内容
  5. toast.show()显示内容
  6. toast.setView(View v)

例子

1.只显示图片的Toast

  public void showToast(){
//获取一个Toast对象,为下面操作准备
Toast toast = new Toast(this);
ImageView img = new ImageView(this);
//用系统提供的图片
img.setImageResource(R.drawable.ic_launcher);
//设置图片
toast.setView(img);
toast.show();
}

最后给一个按钮设定一个监听器,在onClick方法中调用对应的showToast方法就可以了。(下面两个例子同样省略这一步)

2.显示图片和文字

public void showToast2(){
Toast toast = Toast.makeText(this, "这是一个有图片的吐司", Toast.LENGTH_LONG);
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
//得到toast的布局对象
LinearLayout toast_layout = (LinearLayout) toast.getView();
//为toast添加图片资源,第二个参数,0表示图片在上
toast_layout.addView(img,1);
toast.show();
}

3.设计自己的Toast

有时候上面两种还没能满足自己的要求,就可以自定义布局(我在drawable中放了两张图片,詹姆斯和库里的)

准备布局文件

准备好你想要展示的Toast布局文件,我在layout文件夹新建了一个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="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="90dp"
android:background="@drawable/c2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:gravity="center"
android:text="VS"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="90dp"
android:background="@drawable/c1"
/>
</LinearLayout>

加载你的布局到Toast对象

public void showMyTosat(){
//把一个布局变成一个View对象
LayoutInflater inflater = LayoutInflater.from(this);
View toast_layout = inflater.inflate(R.layout.toast, null);
//设定自定义的布局
Toast toast = new Toast(this);
toast.setView(toast_layout);
toast.show();
}

显示图片的(自定义)吐司Toast的更多相关文章

  1. 在Toast里面显示图片

        关于怎么在Toast里面显示图片,首先自定义一个toast,在自定义一个布局,这个布局你想让toast显示什么样的布局就定义什么样的,然后在自定的布局中放一个ImageView,在把自己自定义 ...

  2. 自定义吐司以及Toast的各种用法,详解。教你学会使用各种各样的Toast

    package com.david_Custoast; import com.david_Custoast.R; import android.app.Activity; import android ...

  3. Toast的用法(可以设置显示时间,自定义布局的,线程中的Toast)

           自定义的Toast类 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  4. iOS View自定义窍门——UIButton实现上显示图片,下显示文字

    “UIButton实现上显示图片,下显示文字”这个需求相信大家在开发中都或多或少会遇见.比如自定义分享View的时候.当然,也可以封装一个item,上边imageView,下边一个label.但是既然 ...

  5. [android] 手机卫士自定义吐司

    继续在之前监听来电的服务AddressService里,添加成员方法MyToast() 获取TextView对象,new出来,构造参数:上下文对象 调用TextView对象的setText()方法,设 ...

  6. 百度小程序自定义通用toast组件

    百度小程序Toast组件 author: @TiffanysBear 百度小程序自定义通用toast组件 BdToast百度小程序自定义通用组件-github地址 需求 手百小程序的toast仅支持在 ...

  7. Android 自定义吐司通知

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  8. python 读取并显示图片的两种方法

    在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片.本人偏爱 matpoltlib,因为它的语法更像 matlab. 一.matplotlib 1. ...

  9. iOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果

    一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // re ...

随机推荐

  1. 【前端】js代码模拟用户键盘鼠标输入

    js代码模拟用户键盘鼠标输入 原生js var event = new Event('mousewheel'); event.wheelDelta = 360 document.dispatchEve ...

  2. C# 统计在线人数和总访问人数

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. [iOS][issis] requestLocationWithReGeocode AMapLocationErrorDomain Code=5 "取消"

    Tip: IOS 使用高德地图一次定位  (在该博客找到了解决答案) 在定位时如果出现下面这个Error,说明你的locationManager没有设置成全局变量,导致locationManager提 ...

  4. Java开发中经典的小实例-(打印输入重复的值)

    import java.util.ArrayList;import java.util.Scanner;public class Test8 {    public static void main( ...

  5. jsp页面输出序号

    <c:forEach items="${tests}" var="test" varStatus="s"> <li> ...

  6. jmeter+ant+jenkins+mac环境搭建

    一.环境准备 1.JDK环境:http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.ANT环境:http://ant ...

  7. python成长之路【第十四篇】:HTML初步认识

    HTML介绍 HTML是负责描述文档语义的语言.它是纯文本文件,用一些标签来描述文字的语义,这些标签在浏览器里面是看不到的,所以称为"超文本".所以就是"超文本标记语言& ...

  8. golang os/exec 执行外部命令

    exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...

  9. web.xml listener和event

    Listener接口 Event类 ServletContextListener ServletContextEvent ServletContextAttributeListener Servlet ...

  10. jq弹出一个透明小提示窗,然后逐渐消失

      function show_main(content) { var showWindow = '<div id="show_main" style="borde ...