Android中的Toast
来源:互联网 发布:vb程序 编辑:程序博客网 时间:2024/06/14 16:02
Android中的Toast
简介
Toast是一个弹出Message,允许你便捷地通知用户一些时间,比如:将数据保存到SD卡。值得注意的是用户不能取消Toast。大多数情况下,Toast仅仅是一个简短的message,但你也可以定制Toast的界面。
创建标准Toast
标准Toast可以通过Toast的静态方法makeText来创建:
Toast.makeText(getApplicationContext(), "Hello, The Code Project!", Toast.LENGTH_SHORT).show();参数分别为应用上下文,显示的message内容,显示的延迟。你也可以通过R来调用资源文件的内容,如R.string.hello_codeproject。Message显示的延迟可以是LENGTH_SHORT或LENGTH_LONG,默认情况下是LENGTH_SHORT。你也可以通过调用setDuration方法设置延迟。
设置Toast的位置
你可以设置Toast在屏幕上的位置,通过调用如下方法:
Toast toast = Toast.makeText(getApplicationContext(), "Hello, The Code Project!", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();其中第一个参数设置位置,第二个参数定义了相对于第一个参数位置的偏移像素。
在标准Toast中添加图像
你需要创建ImageView对象,并调用setImageResource方法,在Toast中添加图像。
Toast toast = Toast.makeText(getApplicationContext(), "Hello, The Code Project!", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayout toastView = (LinearLayout) toast.getView();ImageView imageCodeProject = new ImageView(getApplicationContext());imageCodeProject.setImageResource(R.drawable.codeprojectlogo);toastView.addView(imageCodeProject, 0);toast.show();
效果如图:
![toast3.png](http://www.codeproject.com/KB/android/toasts-android/toast3.png)
创建定制布局的Toast
首先要创建定制布局的Toast的layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:layout_width="wrap_content"android:background="#ffffffff" android:orientation="vertical"android:id="@+id/llToast" ><TextViewandroid:layout_height="wrap_content"android:layout_margin="1dip"android:textColor="#ffffffff"android:layout_width="fill_parent"android:gravity="center"android:background="#bb000000"android:id="@+id/tvTitleToast" /><LinearLayoutandroid:layout_height="wrap_content"android:orientation="vertical"android:id="@+id/llToastContent"android:layout_marginLeft="1dip"android:layout_marginRight="1dip"android:layout_marginBottom="1dip"android:layout_width="wrap_content"android:padding="15dip"android:background="#44000000" ><ImageViewandroid:layout_height="wrap_content"android:layout_gravity="center"android:layout_width="wrap_content"android:id="@+id/tvImageToast" /><TextViewandroid:layout_height="wrap_content"android:paddingRight="10dip"android:paddingLeft="10dip"android:layout_width="wrap_content"android:gravity="center"android:textColor="#ff000000"android:id="@+id/tvTextToast" /></LinearLayout></LinearLayout>
我们创建的这个Toast,有一个header,一个图像和一段message。现在我们需要把layout应用在我们创建的Toast上:
LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.customtoast, (ViewGroup) findViewById(R.id.llToast));ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);image.setImageResource(R.drawable.codeprojectlogo);TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);title.setText("Attention");TextView text = (TextView) layout.findViewById(R.id.tvTextToast);text.setText("Hello, The Code Project!");Toast toast = new Toast(getApplicationContext());toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();
![toast4.png](http://www.codeproject.com/KB/android/toasts-android/toast4.png)
- Android中的Toast
- android中的toast
- Android中的Toast详解
- Android中的Toast控件
- Android中的Toast提示
- Android中的Toast创建
- android中的Toast
- Android中的Toast
- Android 中的Toast的应用
- Android中的Toast的使用
- Android中的几种Toast
- android之子线程中的Toast
- Android中的Toast的应用
- Android中的Toast和Notification
- Android中的Toast源码分析和自定义Toast
- 关于android中的Toast的用法
- Android中的Toast重复显示的问题
- Android中的互动交流 之 Toast
- PHP如何获取中文字符串长度 utf8_strlen()
- Oracle 数据库实现自增长列
- Webgame的mina聊天服的优化
- 找出list中的不同元素、删除两个list中相同的对象
- httpClient使用方法
- Android中的Toast
- Android Manifest 学习链接
- ArcCatalog连到ArcSDE后,仅显示空间数据表
- requestWindowFeature(featrueId)
- iPhone App Store Top25 Grossing应用一览(20111219)
- Excel精度问题。
- 到底什么情况该选择开发Web App?
- 《卓有成效的程序员》读后感
- SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法