Android Toast使用详解
开发中常见的使用场景主要有以下几种:
- 默认Toast
- 自定义位置Toast
- 带图片的Toast
- *自定义View的Toast
默认Toast
Toast.makeText(getApplicationContext(), "Toast内容", Toast.LENGTH_SHORT).show();
自定义位置Toast
toast = Toast.makeText(getApplicationContext(),"Toast内容", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
带图片Toast
toast = Toast.makeText(getApplicationContext(),"Toast内容", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
自定义ViewToast
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
参考自:http://android.tgbus.com/Android/tutorial/201103/346236.shtml