Android Toast Pee

Source: Internet
Author: User

Summary: Toast English meaning is toast, in Android, it is like baking machine in a good toast, and for a short period of time after slowly disappear.

A toast is also a container that can contain a variety of view and carry them in the display.

Android provides a simple toast message prompt mechanism, you can click on some of the buttons, prompting the user some information, the message can not be clicked by the user, toast information according to the user set the display time automatically disappear. Toast information can be used in debugging programs to display some of the things you want to display, or to provide users with a friendly interface display effect.
There are two ways to create and display toasts:
  
 
  1. Toast.maketext (context context, int resId, int duration)
    Toast.maketext (Context context, charsequence text, int duration)
    The context is usually the current activity;resid is a string string of id,charsequence for the strings you want to display, duration for the displayed time, you can choose Toast.length_ Short or Toast.length_long, you can also customize the time. How to use:
    Toast.makeText(this, "this is string", Toast.LENGTH_SHORT).show();

  2. Create your own toast, and set the view, which is custom

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

You can define a toast yourself with the code above, and we'll show you the picture.

How to set the location of toast display

Method One:

setgravity (int gravity, int xoffset, int yoffset) three parameters (starting position, horizontal right displacement, vertical downward displacement)

Method Two:

setmargin (float horizontalmargin, float verticalmargin) sets the display position in both horizontal and vertical percentages, with parameters of type float (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:

Toast has a public method SetText (), you can set the toast to Resid or string, which can be used in our first method, the second way to customize the toast is not available, the use of the words will throw an exception.

The reason is that the first way to create a toast is to create a view on its own, that is, TextView, and we use this settext actually set the content to this textview, and the custom view will not have this control, and therefore will be 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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.