Android Toast 自定义
<?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:gravity="center"
android:background="@android:color/holo_green_light"
android:orientation="vertical" > <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"/> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"/>
</LinearLayout>
item_toast
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toast1"
android:gravity="center"
android:text="自定义Toast" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toast2"
android:gravity="center"
android:text="Toast.makeText" /> </LinearLayout>
activity_main
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void toast1(View v){
//Toast通过构造方法创建,但是必须手动设置视图(setView、setDuration)
Toast toast = new Toast(this); //动态加载布局
View view = getLayoutInflater().inflate(R.layout.item_toast, null);
TextView tv1 = (TextView) view.findViewById(R.id.tv1);
TextView tv2 = (TextView) view.findViewById(R.id.tv2); //view.setBackgroundResource(R.drawable.ic_launcher);
view.setBackgroundColor(Color.YELLOW);;
tv1.setText("提示");
tv2.setText("再按一次退出"); toast.setView(view);
//Gravity.FILL_HORIZONTAL显示水平铺满,offset代表x,y轴上的偏移
toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.BOTTOM, 0, 500);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show(); } public void toast2(View v){
Toast toast = Toast.makeText(this, "静态方法构建的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.LEFT, 0, 800);
//只有静态方法构建的toast才能用setText()方法
toast.setText("你好吗?");//打印你好吗?
toast.setText(R.string.hello_world);//打印的是Hello world!
toast.show();
} }
MainActivity
Android Toast 自定义的更多相关文章
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- Android Toast效果设置
Android Toast效果设置 Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.总 ...
- 转--android Toast大全(五种情形)建立属于你自己的Toast
Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...
- Android Toast 总结(五种用法)
Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...
- Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View
一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...
- 【转】android Toast大全(五种情形)建立属于你自己的Toast
Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...
- android Toast大全(五种情形)建立属于你自己的Toast
Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...
- Android Toast的多功能封装——Android开发之路1
Android封装实现各种功能的Toast GitHub地址:https://github.com/SibreiaDante/ToastUtils 效果图: 方法封装如下: showSingleton ...
随机推荐
- 依赖注入与Unity(一) 介绍
在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...
- ASPNET5的依赖注入
ASP.NET5设计的时候就是以DI为基础的,它可以利用内建的框架在Startup类的方法中,把依赖注入进去.应用服务也可以被配置的注入.默认的服务容器提供一些基本的功能,它并不打算代替现代主流的DI ...
- (转)PHP的ereg()与eregi()的不同及相同点。对比
ereg() 字符串比对解析. 语法: int ereg(string pattern, string string, array [regs]); 返回值: 整数/数组 函数种类: 资料处理 内容说 ...
- YII框架开发一个项目的通用目录结构
YII框架开发一个项目的通用目录结构: 3 testdrive/ 4 index.php Web 应用入口脚本文件 5 assets/ 包含公开的资源文件 6 css/ 包含 CSS 文件 7 ima ...
- SQL In的替换
前2天在搞SQL的的时候,发现其中有很多in的操作,诸如:id in(1,2,3)或id in(select id from table……),这个对SQL来说并不是最好的选择,执行效率是偏低的[它执 ...
- scn转换为十进制
- iOS 使用xmpp做聊天客户端
可以号称史上最详细的xmpp做iOS客户端聊天介绍. 简介:XMPP协议是一种基于Socket长连接.以XML格式进行基本信息交换.C/S S/S多种架构的聊天协议 XMPPServer 基于XMP ...
- UITableView中的visibleCells的用法(visibleCells帮上大忙了)
这两天遇到一个问题,UITableView中需要加入动画,而且每一行的速度不一样. 刚开始做时把所有的cell都遍历一遍加上动画,后来发现,如果数据很多时,就会出现各种各样的问题,而且没有显示在界 ...
- Swift - 12 - 区间运算符和for-in
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- underscorejs-reject学习
2.9 reject 2.9.1 语法: _.reject(list, predicate, [context]) 2.9.2 说明: 前边我们已经学习了filter方法,那么我们在学习reject之 ...