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();
效果
建立定製布局的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();