Custom Toast display position and content, custom toast

Source: Internet
Author: User

Custom Toast display position and content, custom toast

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 xmlns: app = "http://schemas.android.com/apk/res-auto" 4 xmlns: tools = "http://schemas.android.com/tools" 5 android: layout_width = "match_parent" 6 android: layout_height = "match_parent" 7 android: orientation = "vertical" 8 tools: context = "com. example. leeson7_1_id19.MainActivity "> 9 10 <Button11 android: onClick =" toast_1 "12 android: layout_width =" match_parent "13 android: layout_height =" wrap_content "14 android: text = "normal toast"/> 15 <Button16 android: onClick = "toast_2" 17 android: layout_width = "match_parent" 18 android: layout_height = "wrap_content" 19 android: text = "Custom toast"/> 20 <Button21 android: onClick = "toast_3" 22 android: layout_width = "match_parent" 23 android: layout_height = "wrap_content" 24 android: text = "Custom location toast"/> 25 </LinearLayout>
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <TextView 8 android: layout_width = "match_parent" 9 android: layout_height = "wrap_content" 10 android: text = "Custom location text" 11 android: gravity = "center" 12 android: background = "# ff0000" 13 android: padding = "5dp" 14 android: textSize = "18sp"/> 15 16 17 </LinearLayout>
1 package com. example. leeson7_mongoid19; 2 3 import android. OS. bundle; 4 import android. support. v7.app. appCompatActivity; 5 import android. view. gravity; 6 import android. view. view; 7 import android. widget. imageView; 8 import android. widget. toast; 9 10 public class MainActivity extends AppCompatActivity {11 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main); 16} 17 18 public void toast_1 (View V) {19 // set the content and duration of the Toast and display 20 Toast. makeText (this, "I am a normal toast", Toast. LENGTH_SHORT ). show (); 21} 22 public void toast_2 (View V) {23 // custom Toast 24 // create toast 25 Toast = new toast (this ); 26 // set the length of time for display of the toast to 27 toast. setDuration (Toast. LENGTH_SHORT); 28 // create ImageView29 ImageView img = new ImageView (this); 30 // set the image resource path 31 img. setImageResource (R. mipmap. ic_launcher); 32 // sets the toast for the view image of the toast. setView (img); 34 // display toast 35 toast. show (); 36} 37 public void toast_3 (View V) {38 // custom display position 39 // create Toast 40 toast Toast = new Toast (this ); 41 // locate the toast layout position 42 View layout = View. inflate (this, R. layout. toast, null); 43 // set toast text, and pass the set layout to 44 toast. setView (layout); 45 // set the position where the toast is displayed on the screen. 46 toast. setGravity (Gravity. FILL_HORIZONTAL | Gravity. TOP,); 47 // display toast 48 toast. show (); 49} 50}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.