一、什么是Toast

1、Toast是一种提供给用户简洁提示信息的视图。

2、该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示。

3、Android提供的Toast类可以创建和显示Toast信息。

下面演示4种Toast的用法,普通的 Toast,更改位置的Toast,显示图片的Toast,自定义的Toast分别使用4个按钮来实现。

main.xml

<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"
tools:context=".MainActivity">
<Button
android:id="@+id/btnShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast显示"/> <Button
android:id="@+id/btnShow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast显示位置"/> <Button
android:id="@+id/btnShow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast显示图片"/> <Button
android:id="@+id/btnShow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义Toast显示layout文件"/> </LinearLayout>

main.java

package com.example.zhengcheng.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast; public class MainActivity extends Activity { Button btnshow1;
Button btnshow2;
Button btnshow3;
Button btnshow4; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshow1 = (Button) findViewById(R.id.btnShow);
btnshow1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "测试Toast弹出消息框", Toast.LENGTH_SHORT);
toast.show();
}
}); btnshow2 = (Button) findViewById(R.id.btnShow2);
btnshow2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "更改Toast弹出位置", Toast.LENGTH_SHORT);
//第一个参数设置Toast的相对位置,第二个参数为x坐标(正为右,负为左),第三个参数为y坐标(正为下,负为上)
toast.setGravity(Gravity.CENTER, 0, -250);
toast.show();
}
}); btnshow3 = (Button) findViewById(R.id.btnShow3);
btnshow3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, " 带图片的Toast弹出对话框", Toast.LENGTH_SHORT);
//获取toast的布局方式
LinearLayout toast_layout = (LinearLayout) toast.getView();
ImageView iv = new ImageView(MainActivity.this); //创建图片控件
iv.setImageResource(R.mipmap.ic_launcher); //设置显示图片
// toast_layout.addView(iv); //添加图片控件到toast布局中,默认添加到末尾
toast_layout.addView(iv, 0); //添加图片控件到toast布局中,添加到文字之前
toast.show(); //显示图片
}
}); btnshow4 = (Button) findViewById(R.id.btnShow4);
btnshow4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取inflater对象,用来加载视图
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View toast_view = inflater.inflate(R.layout.toast_layout,null);
Toast toast = new Toast(MainActivity.this); //创建一个Toast
toast.setView(toast_view); //设置Toast的视图
toast.show(); //显示
}
});
}
}

自定义Toast视图 toast_layout.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="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="自定义的Toast对话框" /> <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/sym_def_app_icon" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="显示Toast弹出的内容" />
</LinearLayout>

最新文章

  1. 基于RXTX的串口通讯 windows64位系统可用
  2. Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
  3. 用程序集编写clr表值函数:把正则表达式引入数据库中
  4. Hadoop的管理目录
  5. PLSQL 逻辑多线程
  6. FreeMarker 一二事 - 静态模板的使用与生成
  7. Oracle 临时表
  8. 【TOMCAT】Tomcat gzip压缩传输数据
  9. Java学习之Iterator(迭代器)的一般用法
  10. iOS_SN_BlueTooth (二)iOS 连接外设的代码实现
  11. ubuntu 使用第一天
  12. [国嵌攻略][119][Linux中断处理程序设计]
  13. 阿里云API网关(17)签名算法
  14. 【转】Linux中的EAGAIN含义
  15. C#多线程读写同一文件处理
  16. 如何查看 EBS 环境上的 INV RUP 版本号
  17. html基础+常用标签
  18. Internet History, Technology and Security (Week3)
  19. 【assembly】用汇编写的一个BMP图片读取器
  20. Oracle学习笔记之八(几条简明的优化SQL方法)

热门文章

  1. 使序列有序的最少交换次数(minimum swaps)
  2. jQuery遍历文档(重要)
  3. Spinner用法与ListView用法
  4. jQuery操作下拉列表以及单选多选框
  5. Linux内存管理之页面回收【转】
  6. 华为上机测试题(及格分数线-java)
  7. Laravel5.1忽略Csrf验证的方法
  8. Android视频压缩并且上传
  9. POJ 3080-Blue Jeans【kmp,字符串剪接】
  10. 【排序】逆序对IV