Today I would like to briefly talk about the toast in Android, because I just touch, toast of the use of the situation I met not many, the following only some of the situation I have encountered.
First, what is toast
Toast is a mechanism provided by Android to display a message, a bit like a pop-up window, a small box on the screen, the box can be placed inside the content you want to display (text, images can be), the message box will disappear over a period of time.
If you've never used a toast, the description might be too abstract, but you must have seen it in the process of using your phone, but you don't know it's called a toast.
Ii. Basic Usage
Toast.maketext (this. Getapplicationcontext (), "This is a toast!", Toast.length_long). Show ();
Only one line of code is needed to display this is a Toast string on the phone screen.
The Toast.maketext function is used to display text, has three parameters, the first argument is context, the second parameter is the string to display, the third parameter is the length of time to display, and the longer time is chosen here. The following. Show () is the meaning of the display.
To see the effect of the display:
Iii. Custom Usage
Toast t = toast.maketext (this. Getapplicationcontext (), "This is a toast!", toast.length_short);
If we define a toast object named T with such a code, you will find that T has a number of member methods to invoke, for example: setgravity (int gravity, int xoffset, int yoffset), setduration (int Duration) and so on.
1. Custom Location
Using the Setgravity method, you can change the location of the toast display, such as:
T.setgravity (gravity.center,0,0);
Indicates that the display is at the center of the screen because both X and Y offsets are 0.
If you modify the offset, the effect is as follows:
T.setgravity (gravity.center,0,-200);
2. Custom content (add picture)
Toast toast = Toast.maketext (This.getapplicationcontext (), "This is a toast including image!" , Toast.length_long); 0, 0); = (LinearLayout) Toast.getview (); New ImageView (This.getapplicationcontext ()); Imageview.setimageresource (r.drawable.my_image);//Assume that there is a PNG format picture named My_image under the Res/drawable folder, which is loaded in at this time 0 ); Toast.show ();
The advantage of this approach is that you do not need to create another layout file.
The effect is as follows:
3, after learning new knowledge and re-update ...
Android Learning Note (ii)--toast