<?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 自定义的更多相关文章

  1. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  2. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  3. Android Toast效果设置

    Android Toast效果设置 Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.总 ...

  4. 转--android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  5. Android Toast 总结(五种用法)

    Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...

  6. Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View

    一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...

  7. 【转】android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  8. android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  9. Android Toast的多功能封装——Android开发之路1

    Android封装实现各种功能的Toast GitHub地址:https://github.com/SibreiaDante/ToastUtils 效果图: 方法封装如下: showSingleton ...

随机推荐

  1. OpenGL ES 3.0 顶点缓冲区VBO使用

    一般情况下数据都是有CPU从RAM取数据 然后传给GPU去处理,相对于GPU速度要慢一些. 使用VBO技术 可以把数据存储到GPU的内存空间中,这样GPU可以直接从GPU的内存中取得数据进行处理 速度 ...

  2. 启动php-fpm报错:please specify user and group other than root

    安装好PHP之后启动报错: 启动php-fpm报错:please specify user and group other than root, pool 'default 修改 php-fpm.co ...

  3. Css3中的响应式布局的应用

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel= ...

  4. 泛型,迭代器,LinkedList<E>

    1 <e>里面只能填类,不能用基本数据类型,不过integer 这样的的也行 2在模板类(泛型类中)class demo<e>由于不知道e是那个,所有通常都是重写大家都有的to ...

  5. linux 进程数

    一.linux系统支持的最大进程数 限制1:既然系统使用pid_t表示进程号,那么最大进程数不能超过pid_t类型的最大值吧 限制2:使用命令ulimit -u查看系统中限制的最大进程数,我的机器上是 ...

  6. RedHat9上安装jdk

    1.先在windows下载jdk:jdk-6-dlj-linux-i586.bin 2.用ftp上传给linux下 3.chmod 777 jdk-6-dlj-linux-i586.bin 4.将jd ...

  7. 利用php的ob缓存机制实现页面静态化

    利用php的ob缓存机制实现页面静态化 首先介绍一下php中ob缓存常用到的几个常用函数ob_start():开启缓存机制ob_get_contents():获取ob缓存中的内容ob_clean()清 ...

  8. php的session实现

    对于两次http请求,如果第一次http请求的重要数据要被第二次请求获取,办法是将第一次http请求数据保存下来,保存的办法很多,大体上有使用数据库,缓存,文件等等,那么php中的session实现实 ...

  9. jquery easy ui 学习 (3) window 限制在父类窗体内

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. iOS开发网络篇-JSON文件的解析

    一.什么是JSON数据 1.JSON的简单介绍 JSON:是一种轻量级的传输数据的格式,用于数据的交互 JSON是javascript语言的一个子集.javascript是个脚本语言(不需要编译),用 ...