Android Toast 总结
转载别人的写的不错,自己添加点注意事项,号称有5种效果。
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();
完整代码
1.Main,java
- package com.wjq.toast;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Main extends Activity implements OnClickListener {
- Handler handler = new Handler();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.btnSimpleToast).setOnClickListener(this);
- findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
- this);
- findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
- findViewById(R.id.btnCustomToast).setOnClickListener(this);
- findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
- }
- public void showToast() {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(), "我来自其他线程!",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- public void onClick(View v) {
- Toast toast = null;
- switch (v.getId()) {
- case R.id.btnSimpleToast:
- Toast.makeText(getApplicationContext(), "默认Toast样式",
- Toast.LENGTH_SHORT).show();
- break;
- case R.id.btnSimpleToastWithCustomPosition:
- toast = Toast.makeText(getApplicationContext(),
- "自定义位置Toast", Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- break;
- case R.id.btnSimpleToastWithImage:
- 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();
- break;
- case R.id.btnCustomToast:
- 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();
- break;
- case R.id.btnRunToastFromOtherThread:
- new Thread(new Runnable() {
- public void run() {
- showToast();
- }
- }).start();
- break;
- }
- }
- }
2.main,xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
- android:text="默认"></Button>
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:text="自定义显示位置"
- android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
- android:text="带图片"></Button>
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:text="完全自定义"
- android:id="@+id/btnCustomToast"></Button>
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:text="其他线程"
- android:id="@+id/btnRunToastFromOtherThread"></Button>
- </LinearLayout>
3.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>
注意: 使用toast = new Toast(context); 一定要先 toast.setView() ,然后才能toast.Show() 不然会出错。同时,我使用
LinearLayout toastView = (LinearLayout) toast.getView(); 然后再在这个LinearLayout种添加TextView会出错,后来就直接定义一个LinearLayout了。
Android Toast 总结相关推荐
- [Android] Toast问题深度剖析(二)
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者: QQ音乐技术团队 题记 Toast 作为 Android 系统中最常用的类之一,由于其方便的api设计和简洁的交互体验,被我们所广泛采用 ...
- android toast的使用
今天,没有什么可以写的,在网上查找资料,发现toast有很多知识点,所以记录一下. Toast Toast是为了给当前视图显示一个浮动的显示块,它永远不会获得焦点.一般用于提示一些不那么引人注目,但是 ...
- 使用Kotlin的Android Toast
Android Toast is a UI widget that pops up on the screen for some time. It's very similar to a notifi ...
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
- Android Toast cancel和show深入浅出实战
说到Android Toast,几乎都很熟悉吧,下面讲讲怎么实现下面几种场景: 1.连续点击一个按钮,每次都产生一个新的Toast并且调用show方法 问题:触发了toast以后,toast内容会一直 ...
- 自定义 Android toast 字体大小
让Android Toast 居中 可以设置 字体大小 Toast toast = Toast.makeText(this, "这是一个简单的自定义Toast", Toast.LE ...
- Android Toast 自定义显示时长
Android Toast 只支持两种时间 LENGTH_SHORT 2 秒,LENGTH_LONG 3.5 秒,但是有场景需要自定义显示时长就会有问题,所以需要自定义实现,下边是自定义的类,通过定时 ...
- robotframework + appium 获取android toast
android toast 获取主要方式是在出现toast的时候查找元素:xpath=//*[contains(@text,'记同步')] ,该xpath 表示为toast信息含有 "记 ...
- qt自定义控件-模拟Android toast提示窗口
一.前言 好久没写博客了,最近一直写材料,很难受,在家做点小东西,正好遇到了想做的效果,在桌面程序实现Android的toast效果 二.环境 目标机linux,测试机window10 qt5.7 本 ...
- Android Toast类
1. 默认创建 Toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据用户设置的显示时间后自动消失. Toast静态方法makeText(),生成Toast实例,并调用show()方 ...
最新文章
- Real-Time DNA Sequencing from Single Polymerase Molecules
- spring3.0设置定时任务
- 【进阶技巧】如何绘制高颜值XMind思维导图?色彩使用很重要!
- hive 行转列和列转行的方法_Hive超详细存储
- mysql中的类型与java_mysql与java数据类型对应关系
- Linux系统开机启动流程介绍
- cf1009F. Dominant Indices
- SVN更新时报403错误
- 80.简单搭建nodeJS服务,访问本地站点文件
- 打造极致Material Design动画风格Button
- python字符串转为ascii码_Python学习经验:无重复字符的最长子串
- java se下载完怎么启动_【Java SE】如何安装JDK以及配置Java运行环境
- python程序员面试宝典 剑指offer_程序员面试宝典+剑指Offer + 算法100题系列 + 15个经典算法下载...
- 端口扫描工具Namp
- 通信系统中语音信号的仿真分析
- 单片机AD采样的几种算法
- 计算机科学与技术本科知识体系
- react实现markdown编辑器
- css3复习知识点概括1(根据W3S顺序)
- 计算机专业应届毕业生找工作一定要知道的面试题--必背版
热门文章
- 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 总结一下用caffe跑图片数据的研究流程接上篇
- 数字的格式化c语言课程设计,【图片】发几个C语言课程设计源代码(恭喜自己当上技术小吧主)【东华理工大学吧】_百度贴吧...
- 关于webpack升级过后不能打包的问题;
- KnockoutJS 3.X API 第七章 其他技术(3) 延迟更新
- IE下图片切换的时候,图片总是切换不成功---根本问题是IE缓存图片
- 将帐套升级到百万用户纪念版实践教程
- Compiz Check测试Linux桌面3D兼容性
- MOSS 2010:Visual Studio 2010开发体验(13)——列表开发之列表实例
- MPI集群安装、MPI安装