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

创建定制布局的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