当前位置: 代码迷 >> Android >> android Toast大全(5种情形)建立属于你自己的Toast
  详细解决方案

android Toast大全(5种情形)建立属于你自己的Toast

热度:64   发布时间:2016-05-01 20:42:43.0
android Toast大全(五种情形)建立属于你自己的Toast

Toast大全(五种情形)建立属于你自己的Toast

    博客分类:?
  • android进阶

?

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

?

1.默认效果

?

Java代码??收藏代码
  1. Toast.makeText(getApplicationContext(),?"默认Toast样式",Toast.LENGTH_SHORT).show();??

2.自定义显示位置效果

Java代码??收藏代码
  1. toast?=?Toast.makeText(getApplicationContext(),"自定义位置Toast",Toast.LENGTH_LONG);??
  2. toast.setGravity(Gravity.CENTER,?0,?0);??
  3. toast.show();??

?

3.带图片效果

Java代码??收藏代码
  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?ImageView(getApplicationContext());??
  5. imageCodeProject.setImageResource(R.drawable.icon);??
  6. toastView.addView(imageCodeProject,?0);??
  7. toast.show();??

?

4.完全自定义效果

Java代码??收藏代码
  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?Toast(getApplicationContext());??
  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?Runnable()?{??
  2. ????public?void?run()?{??
  3. ???????showToast();??
  4. ????}??
  5. }).start();??

完整代码

①Main.java

Java代码??收藏代码
  1. package?com.wjq.toast;??
  2. ??
  3. import?android.app.Activity;??
  4. import?android.os.Bundle;??
  5. import?android.os.Handler;??
  6. import?android.view.Gravity;??
  7. import?android.view.LayoutInflater;??
  8. import?android.view.View;??
  9. import?android.view.ViewGroup;??
  10. import?android.view.View.OnClickListener;??
  11. import?android.widget.ImageView;??
  12. import?android.widget.LinearLayout;??
  13. import?android.widget.TextView;??
  14. import?android.widget.Toast;??
  15. ??
  16. public?class?Main?extends?Activity?implements?OnClickListener?{??
  17. ????Handler?handler?=?new?Handler();??
  18. ??
  19. ????@Override??
  20. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  21. ????super.onCreate(savedInstanceState);??
  22. ????setContentView(R.layout.main);??
  23. ??
  24. ????findViewById(R.id.btnSimpleToast).setOnClickListener(this);??
  25. ????findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);??
  26. ????findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);??
  27. ????findViewById(R.id.btnCustomToast).setOnClickListener(this);??
  28. ????findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);??
  29. ??
  30. ????}??
  31. ??
  32. ????public?void?showToast()?{??
  33. ????????handler.post(new?Runnable()?{??
  34. ??
  35. ????????????@Override??
  36. ????????????public?void?run()?{??
  37. ????????????????Toast.makeText(getApplicationContext(),?"我来自其他线程!",Toast.LENGTH_SHORT).show();??
  38. ??
  39. ????????????}??
  40. ????????});??
  41. ????}??
  42. ??
  43. ????@Override??
  44. ????public?void?onClick(View?v)?{??
  45. ????????Toast?toast?=?null;??
  46. ????????switch?(v.getId())?{??
  47. ????????case?R.id.btnSimpleToast:??
  48. ????????????Toast.makeText(getApplicationContext(),?"默认Toast样式",??
  49. ????????????Toast.LENGTH_SHORT).show();??
  50. ????????????break;??
  51. ????????case?R.id.btnSimpleToastWithCustomPosition:??
  52. ????????????toast?=?Toast.makeText(getApplicationContext(),"自定义位置Toast",?Toast.LENGTH_LONG);??
  53. ????????????toast.setGravity(Gravity.CENTER,?0,?0);??
  54. ????????????toast.show();??
  55. ????????????break;??
  56. ????????case?R.id.btnSimpleToastWithImage:??
  57. ????????????toast?=?Toast.makeText(getApplicationContext(),"带图片的Toast",?Toast.LENGTH_LONG);??
  58. ????????????toast.setGravity(Gravity.CENTER,?0,?0);??
  59. ????????????LinearLayout?toastView?=?(LinearLayout)?toast.getView();??
  60. ????????????ImageView?imageCodeProject?=?new?ImageView(getApplicationContext());??
  61. ????????????imageCodeProject.setImageResource(R.drawable.icon);??
  62. ????????????toastView.addView(imageCodeProject,?0);??
  63. ????????????toast.show();??
  64. ????????????break;??
  65. ????????case?R.id.btnCustomToast:??
  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. ????????????title.setText("Attention");??
  72. ????????????TextView?text?=?(TextView)?layout.findViewById(R.id.tvTextToast);??
  73. ????????????text.setText("完全自定义Toast");??
  74. ????????????toast?=?new?Toast(getApplicationContext());??
  75. ????????????toast.setGravity(Gravity.RIGHT?|?Gravity.TOP,?12,?40);??
  76. ????????????toast.setDuration(Toast.LENGTH_LONG);??
  77. ????????????toast.setView(layout);??
  78. ????????????toast.show();??
  79. ????????????break;??
  80. ????????case?R.id.btnRunToastFromOtherThread:??
  81. ????????????new?Thread(new?Runnable()?{??
  82. ????????????????public?void?run()?{??
  83. ????????????????????showToast();??
  84. ????????????????}??
  85. ????????????}).start();??
  86. ????????????break;??
  87. ??
  88. ????????}??
  89. ??
  90. ????}??
  91. }??

②main.xml

Java代码??收藏代码
  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. ????android:padding="5dip"??
  7. ????android:gravity="center">??
  8. ??
  9. ????<Button??
  10. ????????android:layout_height="wrap_content"??
  11. ????????android:layout_width="fill_parent"??
  12. ????????android:id="@+id/btnSimpleToast"??
  13. ????????android:text="默认"?/>??
  14. ????<Button??
  15. ????????android:layout_height="wrap_content"??
  16. ????????android:layout_width="fill_parent"??
  17. ????????android:text="自定义显示位置"??
  18. ????????android:id="@+id/btnSimpleToastWithCustomPosition"?/>??
  19. ????<Button??
  20. ????????android:layout_height="wrap_content"??
  21. ????????android:layout_width="fill_parent"??
  22. ????????android:id="@+id/btnSimpleToastWithImage"??
  23. ????????android:text="带图片"?/>??
  24. ????<Button??
  25. ????????android:layout_height="wrap_content"??
  26. ????????android:layout_width="fill_parent"??
  27. ????????android:text="完全自定义"??
  28. ????????android:id="@+id/btnCustomToast"?/>??
  29. ????<Button??
  30. ????????android:layout_height="wrap_content"??
  31. ????????android:layout_width="fill_parent"??
  32. ????????android:text="其他线程"??
  33. ????????android:id="@+id/btnRunToastFromOtherThread"?/>??
  34. ??
  35. </LinearLayout>??

③custom.xml

Java代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout??xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:layout_height="wrap_content"??
  4. ????android:layout_width="wrap_content"??
  5. ????android:background="#ffffffff"??
  6. ????android:orientation="vertical"??
  7. ????android:id="@+id/llToast"?>??
  8. ??
  9. ????<TextView??
  10. ????????android:layout_height="wrap_content"??
  11. ????????android:layout_margin="1dip"??
  12. ????????android:textColor="#ffffffff"??
  13. ????????android:layout_width="fill_parent"??
  14. ????????android:gravity="center"??
  15. ????????android:background="#bb000000"??
  16. ????????android:id="@+id/tvTitleToast"?/>??
  17. ??
  18. ????<LinearLayout??
  19. ????????android:layout_height="wrap_content"??
  20. ????????android:orientation="vertical"??
  21. ????????android:id="@+id/llToastContent"??
  22. ????????android:layout_marginLeft="1dip"??
  23. ????????android:layout_marginRight="1dip"??
  24. ????????android:layout_marginBottom="1dip"??
  25. ????????android:layout_width="wrap_content"??
  26. ????????android:padding="15dip"??
  27. ????????android:background="#44000000"?>??
  28. ????????<ImageView??
  29. ????????????android:layout_height="wrap_content"??
  30. ????????????android:layout_gravity="center"??
  31. ????????????android:layout_width="wrap_content"??
  32. ????????????android:id="@+id/tvImageToast"?/>??
  33. ????????<TextView??
  34. ????????????android:layout_height="wrap_content"??
  35. ????????????android:paddingRight="10dip"??
  36. ????????????android:paddingLeft="10dip"??
  37. ????????????android:layout_width="wrap_content"??
  38. ????????????android:gravity="center"??
  39. ????????????android:textColor="#ff000000"??
  40. ????????????android:id="@+id/tvTextToast"?/>??
  41. ????</LinearLayout>??
  42. ??
  43. </LinearLayout> ?

?

  相关解决方案