1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:gravity= "Center"6 Android:background= "@android: Color/holo_green_light"7 android:orientation= "vertical" >8 9 <TextViewTen Android:id= "@+id/tv1" One Android:layout_width= "Wrap_content" A Android:layout_height= "Wrap_content" - Android:text= "Text1"/> - the <TextView - Android:id= "@+id/tv2" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:text= "Text2"/> - </LinearLayout>
Item_toast
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent" 4 android:orientation= "vertical">5 6 <Button7 Android:layout_width= "Match_parent"8 Android:layout_height= "Wrap_content"9 Android:onclick= "Toast1"Ten android:gravity= "Center" One Android:text= "Custom Toast" /> A <Button - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" the Android:onclick= "Toast2" - android:gravity= "Center" - Android:text= "Toast.maketext" /> - + </LinearLayout>
Activity_main
1 Public classMainactivityextendsActivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 }8 9 Public voidToast1 (View v) {Ten //Toast is created by construction method, but the view must be set manually (Setview, setduration) OneToast toast =NewToast ( This); A - //Dynamically loading layouts -View view = Getlayoutinflater (). Inflate (R.layout.item_toast,NULL); theTextView TV1 =(TextView) View.findviewbyid (R.ID.TV1); -TextView TV2 =(TextView) View.findviewbyid (R.ID.TV2); - - //View.setbackgroundresource (r.drawable.ic_launcher); + View.setbackgroundcolor (color.yellow); -Tv1.settext ("Hint"); +Tv2.settext ("Press again to exit"); A at Toast.setview (view); - //gravity.fill_horizontal Display horizontal overlay, offset on x, Y axis -Toast.setgravity (gravity.fill_horizontal| Gravity.bottom, 0, 500); - toast.setduration (toast.length_short); - toast.show (); - in - } to + Public voidToast2 (View v) { -Toast toast = Toast.maketext ( This, "Toast built by static method", toast.length_short); theToast.setgravity (gravity.left, 0, 800); * //only toast built by static methods can be used with the SetText () method $Toast.settext ("How are You?") ");//print how are you? Panax NotoginsengToast.settext (R.string.hello_world);//The print is Hello world! - toast.show (); the } + A}mainactivity
Android Toast Customization