Toast的四种用法

A toast is a view containing a quick little message for the user.

 

一。default:

   

  1. Toast.makeText(getApplicationContext(), "default",  Toast.LENGTH_SHORT).show();  

 

二。custom location:

  

  1. toast = Toast.makeText(getApplicationContext(),  "user-defined location", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 00);  
  3. toast.show();  

三。custom image:

 

  1. toast = Toast.makeText(getApplicationContext(),   "user-defined toast with image", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 00);  
  3. LinearLayout toastView = (LinearLayout) toast.getView();  
  4. ImageView imageCodeProject = new ImageView(getApplicationContext());  
  5. imageCodeProject.setImageResource(R.drawable.icon);  
  6. toastView.addView(imageCodeProject, 0);  
  7. toast.show();  

四。completely custom:

    LayoutInflater inflater = getLayoutInflater();
  View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

  ImageView image = (ImageView) layout.findViewById(R.id.image);
  image.setImageResource(R.drawable.ic_launcher);
  TextView text = (TextView) layout.findViewById(R.id.text);
  text.setText("Has received the broadcast!");

  Toast toast = new Toast(getApplicationContext());
  toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  toast.setDuration(Toast.LENGTH_LONG);
  toast.setView(layout);
  toast.show();

 

 

 

 

 

你可能感兴趣的:(android,toast,自定义Toast)