Android 中的Toast是一个弹出的提示消息框,以友好的方式提示用户,例如保存数据成功等。
下面以一个例子来说明Toast的使用以及标准的Toast显示方式和自定义的Toast显示方式来提示友好的信息。以下是该Demo的程序结构图:
[1] res/layout目录下的 main.xml源码:
[html] view plain copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="@string/hello"
11. />
12. <Button
13. android:id="@+id/show"
14. android:layout_width="fill_parent"
15. android:layout_height="wrap_content"
16. android:text="Show Toast"/>
17. </LinearLayout>
[2] res/layout目录下的 customtoast.xml源码:
[html] view plain copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_height="wrap_content" android:layout_width="wrap_content"
5. android:background="#ffffffff" android:orientation="vertical"
6. android:id="@+id/llToast" >
7. <TextView
8. android:layout_height="wrap_content"
9. android:layout_margin="1dip"
10. android:textColor="#ffffffff"
11. android:layout_width="fill_parent"
12. android:gravity="center"
13. android:background="#bb000000"
14. android:id="@+id/tvTitleToast" />
15. <LinearLayout
16. android:layout_height="wrap_content"
17. android:orientation="vertical"
18. android:id="@+id/llToastContent"
19. android:layout_marginLeft="1dip"
20. android:layout_marginRight="1dip"
21. android:layout_marginBottom="1dip"
22. android:layout_width="wrap_content"
23. android:padding="15dip"
24. android:background="#44000000" >
25. <ImageView
26. android:layout_height="wrap_content"
27. android:layout_gravity="center"
28. android:layout_width="wrap_content"
29. android:id="@+id/tvImageToast" />
30. <TextView
31. android:layout_height="wrap_content"
32. android:paddingRight="10dip"
33. android:paddingLeft="10dip"
34. android:layout_width="wrap_content"
35. android:gravity="center"
36. android:textColor="#ff000000"
37. android:id="@+id/tvTextToast" />
38. </LinearLayout>
[3] src目录下的 MainActivity.java源码:
[html] view plain copy
1. package com.andyidea.demo;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.view.Gravity;
6. import android.view.LayoutInflater;
7. import android.view.View;
8. import android.view.ViewGroup;
9. import android.widget.Button;
10. import android.widget.ImageView;
11. import android.widget.LinearLayout;
12. import android.widget.TextView;
13. import android.widget.Toast;
14.
15. public class MainActivity extends Activity {
16.
17. Button btn;
18.
19. /** Called when the activity is first created. */
20. @Override
21. public void onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.main);
24.
25. btn = (Button)findViewById(R.id.show);
26. btn.setOnClickListener(new View.OnClickListener() {
27.
28. @Override
29. public void onClick(View v) {
30. //标准方式
31. showToast1();
32. //标准方式上添加图片
33. showToast2();
34. //自定义显示方式
35. showToast3();
36. }
37. });
38. }
39.
40. /**
41. * Basic Standard Toast
42. * 标准提示信息方式
43. */
44. private void showToast1(){
45. toast = Toast.makeText(getApplicationContext(),"Hello, This is Andy!", Toast.LENGTH_LONG);
46. toast.show();
47. }
48.
49. /**
50. * Adding an Image to the Standard Toast
51. * 在标准显示方式基础上添加图片
52. */
53. private void showToast2(){
54. toast = Toast.makeText(getApplicationContext(),"Hello, This is Andy!", Toast.LENGTH_LONG);
55. toast.setGravity(Gravity.CENTER, 0, 0);
56. toastView = (LinearLayout) toast.getView();
57. imageCodeProject = new ImageView(getApplicationContext());
58. imageCodeProject.setImageResource(R.drawable.icon);
59. toastView.addView(imageCodeProject, 0);
60. toast.show();
61. }
62.
63. /**
64. * Creating a Toast with Custom Layout
65. * 创建自定义的提示信息方式
66. */
67. private void showToast3(){
68. inflater = getLayoutInflater();
69. layout = inflater.inflate(R.layout.customtoast,
70. (ViewGroup) findViewById(R.id.llToast));
71. image = (ImageView) layout.findViewById(R.id.tvImageToast);
72. image.setImageResource(R.drawable.page);
73. title = (TextView) layout.findViewById(R.id.tvTitleToast);
74. title.setText("Attention");
75. text = (TextView) layout.findViewById(R.id.tvTextToast);
76. text.setText("Hello, This is Andy!");
77. toast = new Toast(getApplicationContext());
78. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
79. toast.setDuration(Toast.LENGTH_LONG);
80. toast.setView(layout);
81. toast.show();
82. }
83. }
[4] 点击Show Toast按钮查看效果如下:
标准方式
标准方式+图片
自定义显示方式