<?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. JS加入收藏与设置主页

    收藏: <a href="javascript:void(0)" onclick="shoucang(document.title,window.location) ...

  2. StringBuilder - new line.

    //use this to implement platform-cross new-line. StringBuilder sb = new StringBuilder(); sb.append(S ...

  3. OC - 10.使用Quartz2D绘制个性头像

      效果图 将一张图片剪切成圆形 在图片周围显示指定宽度和颜色的边框 实现思路 效果图中主要由不同尺寸的两大部分组成 蓝色的背景区域,尺寸等于图片的尺寸加上边框的尺寸 图片区域,尺寸等于图片的尺寸 绘 ...

  4. jQuery autoResize

    这是一个用jQuery实现的, 自动调整textarea高度, 非常的好!但原作者已经把它的相关描述页面移除了, 这里做个备份吧~但js路径还在:full: http://james.padolsey ...

  5. windows phone使用sharpcompress进行解压压缩文件

    在做移动端时,当我们需要从服务器获得多个文件时,为了节约流量,服务器一般会返回一个压缩包,那我们就是下载完成后,在手机中进行解压到指定位置 SharpCompress就是可以在手机中进行解压一个类库( ...

  6. 安装 php

    1.yum安装php yum install php 2.配置 apache 支持 php a.找到httpd.conf find / -name  httpd.conf b.编辑 httpd.con ...

  7. [jQuery] $.grep使用

    1.$.grep的功能是查找过滤功能的数组,原数组不受影响. 2.参数定义 jQuery.grep( array, function(elementOfArray, indexInArray), [ ...

  8. link与@import

    导入外部样式的两种写法 <link rel="stylesheet" href="xxxx.css"> <style> @import ...

  9. Spark笔记--使用Maven编译Spark源码(windows)

    1. 官网下载源码 source code,地址: http://spark.apache.org/downloads.html 2. 使用maven编译: 注意在编译之前,需要设置java堆大小以及 ...

  10. Scut:参数导入方式(有遗留疑问)

    先上一段代码: public EnvironmentSetting() { var appServer = GetServerSection(); var protocol = GetProtocol ...