<1> Introduction
Toast is a view for users to quickly transmit small messages. The toast class helps you create and display those messages.
When the view is displayed to the user, it looks like floating on the application. It will not receive the mouse focus. Will flash.
Here are two examples: volume control and short messages that have been saved in your settings.
The simplest way is to use this class to call a static method to construct everything you need and return a new toast object.
<2> Create
First, instantiate a toast object with the maketext () method.
This method has three parameters: application context, text information, and toast time.
It returns a correctly initialized toast object. You can use show () to display the toast.
Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show();
However, you can think of different toast locations, or even use your own layout, instead of a simple text information.
<3> locate toast
A standard toast notification appears at the bottom of the screen, centered horizontally. You can use the public void setgravity (INT gravity, int xoffset, int yoffset) method to change the value.
This method requires three parameters: gravity constant, offset in the X direction, and offset in the Y direction;
For example, if you decide to appear in the upper left corner, you can set gravity as follows:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
If you want to push to the right side, you only need to slightly change the value of the second parameter.
If you want to push down, you only need to slightly change the value of the third parameter.
Gravity constant: Open the constant
Package xiaosi. toast; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. widget. toast; public class toastactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Toast = toast. maketext (this, "wish you a Happy Wedding", toast. length_long); toast. setgravity (gravity. top | gravity. left, 0, 0); toast. show ();}}
<4> create a custom toast
If a simple text message is insufficient, you can create a custom Layout for toast.
To create a custom layout, define a view layout in XML or in your application code, and pass the view object to the setview (View) method.
For example:
Create a layout: (toast_layout.xml)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /></LinearLayout>
Note that the linearlayoutid element is "toast_layout ".
Package xiaosi. toast; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. imageview; import android. widget. textview; import android. widget. toast; public class toastactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); layoutinflater Inflater = getlayoutinflater (); view layout = Inflater. inflate (R. layout. toast_layout, (viewgroup) findviewbyid (R. id. toast_layout_root); imageview image = (imageview) layout. findviewbyid (R. id. image); image. setimageresource (R. drawable. ic_launcher); textview text = (textview) layout. findviewbyid (R. id. text); Toast = toast. maketext (this, "wish you a Happy Wedding", toast. length_long); toast. setgravity (gravity. center, 0, 0); toast. setview (layout); toast. show ();}}
First, retrieve layoutinflater through getlayoutinflater () and (or getsystemservice ())
Then, the inflate (INT, viewgroup) method is used to fill the layout in XML. The first parameter is the layout resource ID, and the second parameter is a view.