How to implement a custom toast for Android toast and its descriptionAndroid toast is used to display a message to the user on the phone screen, and the information will automatically disappear after a period of time. The information can be simple text or complex pictures and other content (display a view).
1. Simple usage
Toast.maketext (Midlet.getapplicationcontext (), "User name cannot be empty", Toast.length_long). Show ();
2. Customize the display position effect
Code
Toast = Toast.maketext (Getapplicationcontext (),
"Custom Location Toast", Toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();
3. With picture effect
Code
<span style= "FONT-SIZE:14PX;" >toast=toast.maketext (mainactivity.this, "Hello" +name, Toast.length_long); toast.setgravity (Gravity.CENTER, 0, 0); LinearLayout Toastview = (linearlayout) toast.getview (); Toastview.setbackgroundcolor (R.layout.toast_hud); ImageView imagecodeproject = new ImageView (mainactivity.this); Imagecodeproject.setimageresource (R.DRAWABLE.PII); Toastview.addview (imagecodeproject, 0); Toast.show ();</span>
Three, constant
int Length_long
Persistent display of views or text prompts for a longer period of time. The length of the time can be customized. setduration (int)
int Length_short Displays the view or text hint for a short time. The length of the time can be customized. The value is the default value.
Four, the constructor function
Public toast (context) constructs an empty toast object. Before calling show (), you must first call Setview (View).
(Translator Note: Only use Setview (View) to get Toast objects using the new toast), or you must use the Maketext () method to create the Toast object, and
You cannot use the SetText () method in this way to get a toast object. )
the context to which the parameter context is used. This is usually your application or Activity object.
V. Public methods
public int Cancel ()
If the view is already displayed, it is turned off and no longer displayed. You generally do not need to call this method. Under normal circumstances, the view disappears after the surviving period.
public int getduration () returns the duration see setduration (int)
public int getgravity () gets the location where the hint information appears on the screen. See Gravity setgravity ()
public float Gethorizontalmargin () returns the blank space outside the horizontal bar.
public float Getverticalmargin () returns the blank space outside the vertical column.
Public Viewgetview () returns the View object. See Setview (View)
public int Getxoffset () returns the horizontal offset pixel amount relative to the reference position.
Toast msg = Toast.maketext (main.this, "Message", Toast.length_long); Msg.setgravity (Gravity.center, Msg.getxoffset ()/2, Msg.getyoffset ()/2); msg.show ();
public int Getyoffset ()
Returns the amount of vertical offset pixels relative to the reference position.
public static Toastmaketext (context context, int resId, int duration)
Generates a standard Toast object from a resource that contains a text view.
Parameter context
The context to use. This is usually your application or activity object.
ResId
The string resource ID to use, which can be formatted text.
Duration
The duration of the information. Value is Length_short or Length_lon exception
Throws an exception when the resource is not found Resources.notfoundexception public static Toastmaketext (context context, charsequence text, int duration)
Produces a standard Toast object that contains a text view. Parameter context
The context to use. This is usually your application or activity object.
Resid the text to display, which can be formatted text.
Duration The duration of this information. Value is Length_short or Length_long
public void setduration (int duration)
Set the duration of the lifetime. Note: The setting is too large to work, the actual available maximum value of duration is 3500, which can only display up to 3.5s
See Length_short Length_long
public void setgravity (int gravity, int xoffset, int yoffset)
Sets the display position of the cue information on the screen.
(Translator Note: The display location of a custom toast, such as toast.setgravity (gravity.center_vertical, 0, 0), can be positioned in the upper left corner of the toast. Location of toast hint Xoffset: Move more than 0 to the right, less than 0 to the left)
See Gravity Getgravity ()
public void SetMargin (float horizontalmargin, float verticalmargin)
Sets the outer space of the view.
Parameter horizontalmargin the edge of the container and the horizontal white space of the hint information (ratio to the container width).
VerticalMargin the edge of the container and the vertical padding of the hint information (ratio to the container height).
public void SetText (int resId)
Updates the text content of the Toast object that was generated before the Maketext () method.
The parameter resId the new string resource ID specified for the Toast.
public void SetText (Charsequence s)
Updates the text content of the Toast object that was generated before the Maketext () method.
The parameter S is the new text specified for the Toast.
public void Setview (view view)
Sets the view to display.
(Translator Note: Note that this method can display a custom toast view that can contain images, text, and so on.) is a more commonly used method. )
Specific implementation:
Mainactivity.java
<span style= "FONT-SIZE:14PX;" >package Com.example.sahello1;import Android. R.color;import Android.os.bundle;import Android.annotation.suppresslint;import Android.app.Activity;import Android.view.gravity;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity {private TextView btnclick; Private EditText Etname; Private Toast Toast=null; @SuppressLint ("Resourceascolor") @Overrideprotected void OnCreate (Bundle savedinstancestate ) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); btnclick= (TextView) findViewById ( R.id.btnclick); etname= (EditText) Findviewbyid (r.id.edname); Btnclick.setonclicklistener (new Onclicklistener () { public void OnClick (View v) {String name=etname.gettext (). ToString ();/*toast=toast.maketext (mainactivity.this, "Hello" +name, Toast.length_long); toast.setgravity (Gravity.CENTER, 0, 0); LinearLayout Toastview = (linearlayout) Toast.getview (); Toastview.setbackgroundcolor (R.layout.toast_hud); ImageView imagecodeproject = new ImageView (mainactivity.this); Imagecodeproject.setimageresource (R.DRAWABLE.PII); Toastview.addview (imagecodeproject, 0); Toast.show (); */mytoast.mytosat (Mainactivity.this, R.drawable.pii, "show Pictures" +name, 0); Toast1.midtoast (Mainactivity.this, "Show Picture" +name, Toast.length_long);});}} </span>Mytoast.java
<span style= "FONT-SIZE:14PX;" >package Com.example.sahello1;import com.example.sahello1.r.drawable;import Android. R.color;import Android.content.context;import Android.content.res.resources;import Android.graphics.Color;import Android.graphics.drawable.drawable;import Android.view.gravity;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;public class MyToast {public static void Mytosat (context context, int imageId, String content, int duration) {//new A toast passing in the context of the activity to be displayed Toast toast = new toast (context); The displayed time toast.setduration (duration); The displayed position is toast.setgravity (Gravity.bottom, 20, 500); Re-layout the toast linearlayout toastlayout = new LinearLayout (context); Toastlayout.setorientation (linearlayout.horizontal); Toastlayout.setgravity (gravity.center_vertical); Toastlayout.layout (100, 100, 50, 20); ImageView ImageView = new ImageView (context); Imageview.setimageresource (IMAGEID); Add the ImageView to the Toastlayout layout Toastlayout.addview (ImageView); TextView TextView = new TextView (context); Textview.settext (content); Textview.setbackgroundcolor (Color.gray); Resources resources=context.getresources (); Drawable drawable=resources.getdrawable (r.drawable.bg_to); Textview.setbackgrounddrawable (drawable); Add the TextView to the Toastlayout layout Toastlayout.addview (TextView); Toastlayout.setbackgroundcolor (color.red); Add the toastlayout to the Toast Layout toast.setview (toastlayout); Toast.show (); }}</span>UI LayoutActivity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:background=" @drawable/picture "> <edittext android:id=" @+id/edname "Androi D:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" Android:layout_centervertical= "true" android:background= "@android:d rawable/edit_text" Android:drawablel eft= "@drawable/pii" android:drawablepadding= "10DP" android:ems= "android:text=" Please enter "> &L T;requestfocus/> </EditText> <texTView android:id= "@+id/tvnameprompt" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "Android:layout_above=" @+id/edname "android:layout_centerhorizontal=" true "android:text=" @str Ing/tvnameprompt "android:textsize=" 20DP "/> <textview android:id=" @+id/btnclick "Android:la Yout_width= "80DP" android:layout_height= "40DP" android:layout_alignright= "@+id/tvnameprompt" Android: layout_below= "@+id/edname" android:layout_margintop= "27DP" android:background= "@drawable/login_btn_bg" android:gravity= "center" android:paddingbottom= "10DP" android:paddingtop= "10DP" android:text= "click" Android:textcolor= "#FFFFFF" android:textsize= "18SP"/></relativelayout>
login_btn_bg.xml button Effect XML
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/mc_btn_top_pressed "android:state_pressed=" true "/> <!--pressed-- <item android:drawable= "@drawable/mc_btn_top_pressed" android:state_selected= "true"/ > <!--focused-- <item android:drawable= "@drawable/mc_btn_top_unpressed"/> <!-- Default- </selector></span>
Android's Xi. Toast Custom Toast implementation method, and its description