What is Toast1. A toast is a view that provides users with a concise hint of information. 2. The view is presented to the user in the form of hovering over the application. The toast prompt interface does not get the focus, so it does not affect the user's actions. A toast prompt is a way to give a user some information while not affecting the user's use of the program. 3. The toast class provided by Android can create and display the toast information. Toast Common Method Toast.maketext (context,text,duration); The return value is toasttoast.setduration (duration); Set duration toast.setgravity (gravity,xoffset,yoffset); Set Toast location Toast.settext (s); Set prompt content Toast.show (); Show
Toast.setgravity (Gravity,x0ffset,y0ffset); Gravity: Select position by gravity.xxxx, note gravity do not miss X0ffset: X-axis relative to current position (landscape) Offset Y0ffset: Offset from the y-axis (portrait) relative to the current position
1 Public classMainactivityextendsactionbaractivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.fragment_main);7 initevent ();8 9 }Ten One //Initialize Click events A Private voidinitevent () { - //TODO auto-generated Method Stub -Findviewbyid (R.id.button1). Setonclicklistener (NewOnclicklistener () { the - @Override - Public voidOnClick (View v) { - //TODO auto-generated Method Stub + ShowToast1 (); - } + }); A } at - //Show Default Toast - Private voidShowToast1 () { - //Toast Toast=toast.maketext (This, - //R.string.app_name,toast.length_long); -Toast toast = Toast.maketext ( This, "Default Toast", Toast.length_long); in toast.show (); - } to + //Show toast at a custom location - Private voidShowToast2 () { the //Toast Toast=toast.maketext (This, * //R.string.app_name,toast.length_long); $Toast toast = Toast.maketext ( This, "Toast of Change position", Toast.length_long);Panax Notoginseng //toast.setgravity (gravity,x0ffset,y0ffset); - //Gravity: Choose position by gravity.xxxx, note gravity don't miss out the //X0ffset: The offset of the x-axis (landscape) relative to the current position (negative left-to-right), + //Y0ffset: Offset from the y-axis (portrait) relative to the current position (negative upward positive downward) AToast.setgravity (gravity.center, 0, 0); the + toast.show (); - } $ $ //show toast with pictures - Private voidShowToast3 () { - //Toast Toast=toast.maketext (This, the //R.string.app_name,toast.length_long); -Toast toast = Toast.maketext ( This, "Toast with pictures", Toast.length_long);WuyiLinearLayout toastlayout =(LinearLayout) Toast.getview (); theImageView ImageView =NewImageView ( This); - Imageview.setimageresource (r.drawable.ic_launcher); Wu -Toastlayout.addview (ImageView, 0);//The picture is above the text About toast.show (); $ } - - //Custom Toast - Private voidShowToast4 () { A //Toast Toast=toast.maketext (This, + //R.string.app_name,toast.length_long); theLayoutinflater Inflater = Layoutinflater.from ( This); -View Toastview = inflater.inflate (R.layout.toast,NULL); $Toast toast =NewToast ( This); the Toast.setview (toastview); the toast.show (); the } the}
The code for the custom toast. Note that this is Setview (), not AddView (). In addition, inside the r.layout.toast_layout is our custom toast layout. In this case, there is a imageview, and there is a textview on top and bottom of it.
Android Siege Lion re-recognize toast