使用Toast进行用户提醒(转)
Toast是Android提供的一个轻量级的用户提醒控件,使用也很简单,就相当一个极简的dialog!!!下面将向您介绍一些Toast的详细用法:
1、普遍使用的方法:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration);
toast.show();
一般情况下,我们都是这样使用Toast的,就跟其他的UI一样,初始化一个UI需要传入一个Context,这里是通过getApplicationContext获取应用程序的上下文!!!
2、设置Toast显示的位置:
一般情况下,Toast显示在屏幕的下半屏幕中,就像下图所示的那样:
我们可以通过代码更新Toast显示的位置:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
方法原型:
public void setGravity(int gravity, int xOffset, int yOffset)
这里的参数意义就不介绍,相信您根据名字就可以猜出来!!!
改变位置后的Toast:
3、自定义Toast的Layout:
Toast的布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
在代码中解析layout,并将解析的布局添加至Toast中,具体代码如下所示:
public void onShowCustomToast(View view) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
代码运行效果:
使用Toast进行用户提醒(转)的更多相关文章
- 【完全开源】知乎日报UWP版:增加Live磁贴、Badge、以及Toast通知
目录 说明 实现方法 APP生命期 后台任务 说明 之前网上有人建议增加磁贴(tile).徽章(badge)功能.利用周末的时间,将这两个功能添加上去了.如果将磁贴固定到开始屏幕,磁贴就会循环播放首页 ...
- Android开发——你真的了解Dialog、Toast和Snackbar吗
0. 前言 今天给大家带来一篇简单易懂的关于Android提醒小功能的文章.Dialog和Toast我们都不陌生,而Snackbar是Design Support库中提供的新控件,有些朋友可能还不了解 ...
- Android 更改 Toast 的默认位置
Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适.比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没 ...
- android 该项目的优化toast优化技巧
我们这样做的时候经常登录认证使用toast提示用户输入出现错误等..很多人都直接使用 Toast.makeText(LoginActivity.this, "请联系小区的物业管理" ...
- Toast与Snackbar的那点事
背景 Toast是Android平台上的常用技术.从用户角度来看,Toast是用户与App交互最基本的提示控件:从开发者角度来看,Toast是开发过程中常用的调试手段之一.此外,Toast语法也非常简 ...
- UWP开发:获取用户当前所在的网络环境(WiFi、移动网络、LAN…)
原文:UWP开发:获取用户当前所在的网络环境(WiFi.移动网络.LAN-) UWP开发:获取用户当前所在的网络环境: 在uwp开发中,有时候,我们需要判断用户所在的网络,是WiFi,还是移动网络,给 ...
- 2015最流行的Android组件、工具、框架大全
Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件. 本文试图将目前流行的组件收集起来以 ...
- 【Win 10 应用开发】打印UI元素
Windows App支持将UI界面进行打印的功能,这与浏览器中的打印网页的用途相近,其好处就是“所见即所得”,直接把界面上呈现的内容打印下来,比重新创建打印图像方便得多. 要在通用App中实现打印, ...
- 转载 Android 多线程处理之多线程用法大集合
handler.post(r)其实这样并不会新起线程,只是执行的runnable里的run()方法,却没有执行start()方法,所以runnable走的还是UI线程. 1.如果像这样,是可以操作ui ...
随机推荐
- LA 2191 - Potentiometers
看题传送门 Fenwick树的应用~~~ #include <cstdio> #include <cstring> #include<algorithm> usin ...
- POJ 2823 Sliding Window 线段树
http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...
- [Angular] Separating Structural Styles From Theme Styles - Making Components Themeable
For the component's css file, we can improt two css files: common.css default-theme.css @import &quo ...
- Classification and Representation
Classification To attempt classification, one method is to use linear regression and map all predict ...
- Java 字符转Unicode
static String unicode2String(String unicodeStr) { StringBuffer sb = new StringBuffer(); String str[] ...
- 【42.59%】【codeforces 602A】Two Bases
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- [Vue] Update Attributes, Classes and Styles in Vue.js with v-bind
Use v-bind:class and v-bind:style to compute html classes and inline styles from component data. Vue ...
- Linux网络编程——原始套接字实例:MAC 头部报文分析
通过<Linux网络编程——原始套接字编程>得知,我们可以通过原始套接字以及 recvfrom( ) 可以获取链路层的数据包,那我们接收的链路层数据包到底长什么样的呢? 链路层封包格式 M ...
- skip-slave-start的重要性
原来做复制的主机因为数据丢失需要重新创建复制环境,机器上已经有了主库数天前的备份,于是删除数据目录直接把备份放上去,结果发现复制没有抱错,show slave status一切正常,select co ...
- 利用PS把多张psd格式的图片转换为一张PDF格式
最近为公司做了一版电子样册,所有图片都是包含多图层高清晰的psd格式,要做成一个PDF文件的电子样册,发给客户看,面对这些零散的图片,本来打算利用在线合成:在线网址 https://smallpdf. ...