Custom Toast, android custom toast
1. Define a layout in the xml file
For example:
<? Xml version = "1.0" encoding = "UTF-8"?> <TextView xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_centerInParent = "true" android: id = "@ + id/toast_by_text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "18sp" android: textColor = "# ffffff" android: paddingTop = "2dp" android: paddingBottom = "2dp" android: gravity = "center" android: layout_marginTop = "3dp" android: layout_marginBottom = "3dp" android: layout_centerHorizontal = "true" android: text = "Please wait"/>
2. Sample Java code
LayoutInflater inflater = activity. getLayoutInflater (); View layout = inflater. inflate (R. layout. test_toast, null); TextView textView = (TextView) layout. findViewById (R. id. toast_by_text); textView. setText ("Please wait"); Toast toast = new Toast (activity); toast. setGravity (Gravity. CENTER_VERTICAL, 0, 0); toast. setDuration (Toast. LENGTH_SHORT); toast. setView (layout); toast. show ();