1, Toast Control:
By looking at the source code, it is found that the principle inside toast is to obtain a View object (TextView) by obtaining a Layoutinflater layout manager through a service context.layout_inflater_service. Set the content to display it
Copy Code code as follows:
public static Toast Maketext (context 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;
}
To define a layout file:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "200dip"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >
<imageview
Android:id= "@+id/iv_my_toast"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/notification"/>
<textview
Android:id= "@+id/tv_my_toast"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "18SP"
android:text= "Text"
/>
</LinearLayout>
Custom Mytoast class:
Copy Code code as follows:
public class Mytoast {
/**
* Show Custom Toast
* @param context
* @param ID of the iconID icon
* @param text displayed
*/
public static void Showtoast (context context,int iconID, String text) {
View view = view.inflate (context, r.layout.my_toast, null);
TextView TV = (TextView) View.findviewbyid (r.id.tv_my_toast);
ImageView IV = (ImageView) View.findviewbyid (r.id.iv_my_toast);
Iv.setimageresource (iconID);
Tv.settext (text);
Toast Toast = new Toast (context);
Toast.setduration (0);
Toast.setview (view);
Toast.show ();
}
}