Brief introduction: Toast English meaning is toast, in Android. It's like a toast made in a baking machine, and it disappears after a short period of time.
A toast is also a container that can include a variety of view and carry them in the display.
A simple Toast message prompt box mechanism is available in Android. After the user has clicked on some button. Prompt the user for some information. Prompt information can not be clicked by the user, toast information according to the user set the display time to voluntarily disappear. The toast information can be easily displayed when debugging the program, or to provide users with a friendly interface display effect.
There are two ways to create and display toasts:
Toast.maketext (context context, int resId, int duration)
Toast.maketext (Context context, charsequence text, int duration)
The context is the id,charsequence of the string that you want to display, usually for the current ACTIVITY;RESID string. Duration is the time that is displayed, the ability to select Toast.length_short or Toast.length_long, or to define your own time. Usage:
Toast.makeText(this, "this is string", Toast.LENGTH_SHORT).show();
Create your own toast, and set the view, which you define yourself
123456789Ten One A - - |
Toast toast = new Toast (this); //define a ImageViewImageView ImageView = new ImageView (this); Imageview.setimageresource (R.drawable.ic_launcher); //Define a layout, here is layoutlinearlayout Layout = new LinearLayout (this); layout.setorientation (linearlayout.horizontal); //Put the ImageView in LayoutLayout.addview (ImageView); //Set ViewToast.setview (Layout); //Set display timetoast.setduration (20); toast.show ();
|
We can define a toast by the code above, and we also show the picture in it.
How to set the location of toast displayMethod One:
setgravity (int gravity, int xoffset, int yoffset) three parameters (starting position, horizontal right shift, vertical downward displacement)
Method Two:
setmargin (float horizontalmargin, float verticalmargin) sets the display position in both horizontal and vertical percentages, with the number of float types (horizontal displacement positive right negative left, vertical displacement positive negative)
The default gravity is gravity.center_horizontal| Gravity.bottom
Toast toast = Toast.maketext (This, "set your gesture firstly", toast.length_long);Toast Tips
Toast.setgravity (gravity.center_horizontal| Gravity.bottom, 0, 0);Set Center of Gravity first
Toast.setmargin (0f, 0.1f);Set relative center of gravity position
Toast.show ();
Precautions:There is a public method SetText () in the toast that can be set to toast Resid or string, which can be used in our first method. Another way to define a toast yourself is to not be able to use it and throw an exception if you use it.
The reason for this is to use the first method of creation. The toast creates a view, that is, TextView, and we use this settext to actually set the content to this textview, and the view that we define does not have this control. This will result in an error.
//Plus set locationToast toast = Toast.maketext (This, "set your gesture firstly", toast.length_long); //toast hint//toast.setgravity (Gravity.bottom, 20, 60); Toast.show ();
ref:http://blog.csdn.net/pipisorry/article/details/26837809
http://blog.isming.me/blog/2014/03/22/toast-use/
Http://android.codemach.com/zai-android-zhong-shi-yong-toast-jin-xing-ti-shi.html
Android Toast Pee