当前位置: 代码迷 >> Android >> Android中的Toast的基准格式和自定义格式
  详细解决方案

Android中的Toast的基准格式和自定义格式

热度:90   发布时间:2016-05-01 11:28:41.0
Android中的Toast的标准格式和自定义格式

?Android 中的Toast是一个弹出的提示消息框,以友好的方式提示用户,例如保存数据成功等。

下面以一个例子来说明Toast的使用以及标准的Toast显示方式和自定义的Toast显示方式来提示友好的信息。以下是该Demo的程序结构图:

[1] res/layout目录下的 main.xml源码:

[html]?view plaincopy
  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 plaincopy
  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>??
  39. </LinearLayout>??

[3] src目录下的 MainActivity.java源码:

[html]?view plaincopy
  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. [email protected]
  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. [email protected]
  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?=?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?=?Toast.makeText(getApplicationContext(),"Hello,?This?is?Andy!",?Toast.LENGTH_LONG);??
  55. ????????toast.setGravity(Gravity.CENTER,?0,?0);??
  56. ????????LinearLayout?toastView?=?(LinearLayout)?toast.getView();??
  57. ????????ImageView?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. ????????LayoutInflater?inflater?=?getLayoutInflater();??
  69. ????????View?layout?=?inflater.inflate(R.layout.customtoast,??
  70. ???????????(ViewGroup)?findViewById(R.id.llToast));??
  71. ????????ImageView?image?=?(ImageView)?layout.findViewById(R.id.tvImageToast);??
  72. ????????image.setImageResource(R.drawable.page);??
  73. ????????TextView?title?=?(TextView)?layout.findViewById(R.id.tvTitleToast);??
  74. ????????title.setText("Attention");??
  75. ????????TextView?text?=?(TextView)?layout.findViewById(R.id.tvTextToast);??
  76. ????????text.setText("Hello,?This?is?Andy!");??
  77. ????????Toast?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按钮查看效果如下:

标准方式

标准方式+图片

自定义显示方式

  相关解决方案