Toast 的使用1
2015-10-15 19:31
471 查看
Toast是一个浮在屏幕上,展示消失用的,例如当用户输入非法字符时,显示一条提示信息。Toast是永远不能获取焦点的,所以除了显示信息外别无它用。
当然常见的这样:
第一个参数是Context,不用多说;第二个是显示的消息内容;第三个是设置这个Toast存在时间,给出了二个常量,一个是Toast.LENGTH_SHORT,一个是Toast.LENGTH_LONG。而且必须这样设置。
默认的Toast显示在底部的水平中间,向下偏移64dip。
如果我们需要自定义Toast的显示样式,只需进行下面的操作:
1.创建一个布局,命名为layout_toast:
显示的结果为:
简单的使用
我们要简单的使用Toast只需这样写:Toast toast = Toast.makeText(this, "默认的Toast", Toast.LENGTH_SHORT); toast.show();
当然常见的这样:
Toast.makeText(this,"默认的Toast",Toast.LENGTH_SHORT).show();
第一个参数是Context,不用多说;第二个是显示的消息内容;第三个是设置这个Toast存在时间,给出了二个常量,一个是Toast.LENGTH_SHORT,一个是Toast.LENGTH_LONG。而且必须这样设置。
默认的Toast显示在底部的水平中间,向下偏移64dip。
自定义Toast显示的View
如果我们需要自定义Toast的显示样式,只需进行下面的操作:1.创建一个布局,命名为layout_toast:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="自定义的消息" android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>2.使用Toast的setView()设置view
Toast toast = new Toast(this); View view = getLayoutInflater().inflate(R.layout.layout_toast,null); toast.setView(view); toast.show();
显示的结果为:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories