当前位置:操作系统 > 安卓/Android >>

Android中的Toast

简介

Toast是一个弹出Message,允许你便捷地通知用户一些时间,比如:将数据保存到SD卡。值得注意的是用户不能取消Toast。大多数情况下,Toast仅仅是一个简短的message,但你也可以定制Toast的界面。

 


创建标准Toast

标准Toast可以通过Toast的静态方法makeText来创建:


Toast.makeText(getApplicationContext(), "Hello, The Code Project!", 
    Toast.LENGTH_SHORT).show(); 
 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 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(); 
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"?> 
<LinearLayout 
    xmlns: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" > 
    <TextView 
        android: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" /> 
    <LinearLayout 
        android: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" > 
        <ImageView 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_width="wrap_content" 
            android:id="@+id/tvImageToast" /> 
        <TextView 
            android: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> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns: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" >
 <TextView
  android: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" />
 <LinearLayout
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/llToastContent"
  android:layout_marginLeft="1dip"
  android:layout_marginRight="1dip"
  android:layout_margin

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,