Android learning notes-custom Toast and androidtoast
(1) layout
<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: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: layout_marginLeft = "104dp" android: layout_marginTop = "70dp" android: text = "normal Toast"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignRight = "@ + id/button1" android: layout_below = "@ + id/button1" android: layout_marginTop = "50dp" android: text = "Custom Toast"/> </RelativeLayout>
(2) The Toast layout file to be popped up
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#DAA" android:orientation="horizontal" android:padding="8dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:src="@drawable/in" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /></LinearLayout>
(3) class files
Package com. lc. toastdialog; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private Button button1; private Butt On button2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2);/** common Toast */button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, "common Toast", Toa St. LENGTH_SHORT ). show () ;}});/** custom Toast */button2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// find the toast layoutView layout = LayoutInflater. from (MainActivity. this ). inflate (R. layout. toast, null); // set the value textView TextView = (textView) layout in the toast file. findViewById (R. id. text); textView. setText ("hello world! "); Toast toast = new Toast (MainActivity. this); toast. setGravity (Gravity. CENTER_VERTICAL, 0, 0); toast. setDuration (Toast. LENGTH_SHORT); toast. setView (layout); toast. show () ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}