Android中的Toast – Android移動開發技術文章_手機開發 Android移動開發教學課程

簡介

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_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>


我們創建的這個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(); 
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

摘自 xinem的專欄
 

發佈留言