下面這段代碼能夠顯示一個帶圖片的Toast效果:
Java代碼
源碼打印?
<span style="font-size:18px;">// 帶圖片的Toast
Button btn2 = (Button) findViewById(R.id.toast2);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 定義一個Toast
Toast toast = Toast.makeText(ToastDemoActivity.this, "這是一個代圖片的Toast!", Toast.LENGTH_LONG);
// 定義一個ImageView
ImageView imageView = new ImageView(ToastDemoActivity.this);
imageView.setImageResource(R.drawable.icon);
// 得到Toast的View
View toastView = toast.getView();
// 定義一個Layout,這裏是Layout
LinearLayoutlinear Layout = new LinearLayout(ToastDemoActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// 將ImageView和ToastView合併到Layout中
linearLayout.addView(imageView);
linearLayout.addView(toastView);
// 替換掉原有的ToastView
toast.setView(linearLayout);
toast.show();
}
});</span><span style="font-size:16px;">
</span>
this