A Toast is a view that quickly displays a small amount of information to the user. A Toast floats on the application to display information to the user, it never gets the focus, does not affect the user's input, and so on, mainly for some help/hints.
The most common way to create a Toast is to use a static method Toast.maketext
I'm using SDK 2.2.
1. Default display mode
Java code
2. Customizing the display location
Java code
3. With the picture
Java code
1 Toast Toast=toast.maketext (Getapplicationcontext (), "show Toast with pictures", +); 2 toast.setgravity (gravity.center, 0, 0); 3//Create Picture View object 4 ImageView imageview= new ImageView (Getapplicationcontext ()); 5//Set Picture 6 Imageview.setimageresource (r.drawable.ic_launcher); 7//Get the toast Layout 8 linearlayout Toastview = (linearlayout) Toast.getview ();
4. Fully customizable display mode
Java code
1//inflater means inflatable 2//layoutinflater This class is used to instantiate the layout of an XML file to its corresponding View object 3 Layoutinflater inflater = Getlayoutinflater ( ); 4//Fill in a View object by developing an XML file and layout ID 5 View layout = Inflater.inflate (R.layout.custom2, (ViewGroup) Findviewbyid ( R.id.lltoast)); 6 7 ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast); 8//Set picture in Picture view in layout
5. Calls from other threads through Handler
Java code
1//Call Method 1 2//thread th=new Thread (this); 3//th.start (); 4//Call Method 2 5 handler.post (New Runnable () { 6 @Override 7 public void Run () { 8 showtoast ();
Java code
Java code
1 Handler handler=new Handler () { 2 @Override 3 public void Handlemessage (Message msg) { 4 int What=msg.wha t; 5 switch (what) { 6 Case 1: 7 showtoast (); 8 break;
Java code
1 @Override 2 public void Run () {3 handler.sendemptymessage (1); 4}
Original address: http://daikainan.iteye.com/blog/1405575
"Go" "java/android" toast use method Daquan