Today, when I was writing a PDA, I used my own definition of toast.
First look at the source code discovery for Android toast:
/** * Make a standard toast, that just contains a text view. * * @param context the context to use. Usually your {@link android.app.Application} * or {@link android.app.Activity} object. * @param text the text to show. Can be formatted text. * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or * {@link #LENGTH_LONG} * */public static Toast Maketext (Co ntext context, charsequence text, int duration) {Toast result = new toast (context); Layoutinflater inflate = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); View v = inflate.inflate (com.android.internal.r.layout.transient_notification, NULL); TextView TV = (TextView) V.findviewbyid (com.android.internal.r.id.message); Tv.settext (text); Result.mnextview = v; result.mduration = Duration; return result; }Staring, but a toast that includes a textview. It's easy to see that this toast uses a system-brought layout to show the toast, and then uses the Result.mnextview Result.mduration the toast with the number of arguments passed and the layout of the display, knowing the principle we can define our own toast 1, first we want to customize a layout to show that we want to show the effect of Toast 2, Interacting with the components within our own defined layouts by passing the Parameters 3. Connect our layout to our toast by Setview.
<span style= "FONT-SIZE:18PX;" ><?XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android "Android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android : background= "@drawable/toast_shape" > <relativelayout android:layout_width= "match_parent" Androi d:layout_height= "match_parent" android:orientation= "horizontal" android:gravity= "center" > <imageview android:id= "@+id/iv_hint_icon" android:layout_width= "35DP" Android:layout_h eight= "35DP" android:src= "@drawable/pda_failure_icon" android:layout_marginleft= "5DP"/&G T <textview android:id= "@+id/tv_hint_text" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content" android:textstyle= "bold" android:textsize= "28SP" Android:text Color= "#eb4e4e" android:layout_marginleft= "5DP" android:text= "Shelves failed!" android:layout_torightof= "@+id/iv_hint_icon"/> <textview android:id= "@+id/tv_check_add _info "android:layout_below=" @+id/tv_hint_text "android:layout_width=" Wrap_content "Andro id:layout_height= "Wrap_content" android:textstyle= "bold" android:textsize= "22sp" Android: Textcolor= "#eb4e4e" android:layout_margintop= "20DP" android:layout_marginleft= "5DP" Andro id:text= "Please check the listing information carefully!" /> </RelativeLayout></LinearLayout></span>
Next is the defined shape code
<span style= "FONT-SIZE:18PX;" ><?XML version= "1.0" encoding= "Utf-8"?
><shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <corners android:radius= " 10DP "/> <solid android:color=" #ffffff "/> <stroke android:color=" #000000 "android:width=" 2DP "/ > <padding android:bottom= "30DP" android:left= "30DP" android:right= "30DP" android:top= "30DP"/></ Shape></span>
Define your own Toast code
public class Pdatoast extends toast{/** * Construct an empty Toast object. You must call {@link #setView} before-can call {@link #show}. * * @param context the context to use. Usually your {@link application} * or {@link Activity} object. */Public Pdatoast (context context) {super (context); } public static Toast Maketext (Context context,int imgid,charsequence hinttext,boolean showfailure,int duration) { Toast Mtoast = new Toast (context); Layoutinflater Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); View layout = minflater.inflate (r.layout.pda_toast_layout,null),//todo Settings dialog box layout ImageView Hint_icon = (ImageView) l Ayout.findviewbyid (R.id.iv_hint_icon); Hint_icon.setimageresource (Imgid); TextView hint = (TextView) Layout.findviewbyid (R.id.tv_hint_text); Hint.settext (Hinttext); TextView Failure_desc = (TextView) Layout.findviewbyId (R.id.tv_check_add_info); if (showfailure) {failure_desc.setvisibility (view.visible); }else{failure_desc.setvisibility (View.gone); } mtoast.setview (layout); Mtoast.setgravity (gravity.center,0,0); Mtoast.setduration (duration); return mtoast; }}There is one sentence to note:
Construct an empty Toast object. Must call {@link #setView} before * can call {@link #show}
Hint to construct an empty toast, you must call Setview before you show or you will get an error.
public static Toast Maketext (Context context,int imgid,charsequence hinttext,boolean showfailure,int duration)
Explanation of the parameters:
Context: Contexts
Imgid: Displaying picture resources
Hinttext: Display prompt information
Showfailure: Whether an error message is displayed
Duration: Display duration
Call Toast:
<span style= "White-space:pre" ></span> @Override public void onsuccess (Baseresponsedata dataobj , String jsonstring) { pdatoast.maketext (Addactivity.this,r.drawable.pda_success_icon, "shelves succeeded!", false, Toast.length_short). Show (); } @Override public void onfailure (int rescode, string msg, String jsonstring) { log.e ("TAG", "Result:" +rescode + " Error desc: "+msg +" response data: "+jsonstring); Pdatoast.maketext (Addactivity.this,r.drawable.pda_failure_icon, "Shelves failed!", True,toast.length_short). Show ();
Display effect:
That ' s all
Android self-defined toast