Toast大全(五种情形)建立属于你自己的Toast
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。
1.默认效果
1. Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();
2.自定义显示位置效果
1. toast = Toast.makeText(getApplicationContext(),"自定义位置Toast",Toast.LENGTH_LONG);
2. toast.setGravity(Gravity.CENTER, 0, 0);
3. toast.show();
3.带图片效果
1. toast = Toast.makeText(getApplicationContext(),"带图片的Toast",Toast.LENGTH_LONG);
2. toast.setGravity(Gravity.CENTER, 0, 0);
3. LinearLayout toastView = (LinearLayout) toast.getView();
4. ImageView imageCodeProject = new
5. imageCodeProject.setImageResource(R.drawable.icon);
6. toastView.addView(imageCodeProject, 0);
7. toast.show();
4.完全自定义效果
1. LayoutInflater inflater = getLayoutInflater();
2. View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));
3. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
4. image.setImageResource(R.drawable.icon);
5. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
6. title.setText("Attention");
7. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
8. text.setText("完全自定义Toast");
9. toast = new
10. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
11. toast.setDuration(Toast.LENGTH_LONG);
12. toast.setView(layout);
13. toast.show();
5.其他线程
Java代码
1. new Thread(new
2. public void
3. showToast();
4. }
5. }).start();
完整代码
①Main.java
1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15.
16. public class Main extends Activity implements
17. new
18.
19. @Override
20. public void
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.main);
23.
24. this);
25. this);
26. this);
27. this);
28. this);
29.
30. }
31.
32. public void
33. new
34.
35. @Override
36. public void
37. "我来自其他线程!",Toast.LENGTH_SHORT).show();
38.
39. }
40. });
41. }
42.
43. @Override
44. public void
45. null;
46. switch
47. case
48. "默认Toast样式",
49. Toast.LENGTH_SHORT).show();
50. break;
51. case
52. "自定义位置Toast", Toast.LENGTH_LONG);
53. 0, 0);
54. toast.show();
55. break;
56. case
57. "带图片的Toast", Toast.LENGTH_LONG);
58. 0, 0);
59. LinearLayout toastView = (LinearLayout) toast.getView();
60. new
61. imageCodeProject.setImageResource(R.drawable.icon);
62. 0);
63. toast.show();
64. break;
65. case
66. LayoutInflater inflater = getLayoutInflater();
67. View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));
68. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
69. image.setImageResource(R.drawable.icon);
70. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
71. "Attention");
72. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
73. "完全自定义Toast");
74. new
75. 12, 40);
76. toast.setDuration(Toast.LENGTH_LONG);
77. toast.setView(layout);
78. toast.show();
79. break;
80. case
81. new Thread(new
82. public void
83. showToast();
84. }
85. }).start();
86. break;
87.
88. }
89.
90. }
91. }
②main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "vertical"
4. "fill_parent"
5. "fill_parent"
6. "5dip"
7. "center">
8.
9. <Button
10. "wrap_content"
11. "fill_parent"
12. "@+id/btnSimpleToast"
13. "默认"
14. <Button
15. "wrap_content"
16. "fill_parent"
17. "自定义显示位置"
18. "@+id/btnSimpleToastWithCustomPosition"
19. <Button
20. "wrap_content"
21. "fill_parent"
22. "@+id/btnSimpleToastWithImage"
23. "带图片"
24. <Button
25. "wrap_content"
26. "fill_parent"
27. "完全自定义"
28. "@+id/btnCustomToast"
29. <Button
30. "wrap_content"
31. "fill_parent"
32. "其他线程"
33. "@+id/btnRunToastFromOtherThread"
34.
35. </LinearLayout>
③custom.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "wrap_content"
4. "wrap_content"
5. "#ffffffff"
6. "vertical"
7. "@+id/llToast"
8.
9. <TextView
10. "wrap_content"
11. "1dip"
12. "#ffffffff"
13. "fill_parent"
14. "center"
15. "#bb000000"
16. "@+id/tvTitleToast"
17.
18. <LinearLayout
19. "wrap_content"
20. "vertical"
21. "@+id/llToastContent"
22. "1dip"
23. "1dip"
24. "1dip"
25. "wrap_content"
26. "15dip"
27. "#44000000"
28. <ImageView
29. "wrap_content"
30. "center"
31. "wrap_content"
32. "@+id/tvImageToast"
33. <TextView
34. "wrap_content"
35. "10dip"
36. "10dip"
37. "wrap_content"
38. "center"
39. "#ff000000"
40. "@+id/tvTextToast"
41. </LinearLayout>
42.
43. </LinearLayout>
标签:Toast,toast,layout,show,new,import,android,大全
From: https://blog.51cto.com/u_16034393/6625784