`
leiguorui
  • 浏览: 3462 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

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();

 

 

 

 

 

分享到:
评论
1 楼 Evilover3 2012-08-01  
挺好的,要是有实图就更好了

相关推荐

Global site tag (gtag.js) - Google Analytics