转载别人的写的不错,自己添加点注意事项,号称有5种效果。

1.默认效果

代码

        
  1. Toast.makeText(getApplicationContext(),"默认Toast样式",
  2. Toast.LENGTH_SHORT).show();

2.自定义显示位置效果

代码

        
  1. toast=Toast.makeText(getApplicationContext(),
  2. "自定义位置Toast",Toast.LENGTH_LONG);
  3. toast.setGravity(Gravity.CENTER,0,0);
  4. toast.show();

3.带图片效果

代码

        
  1. toast=Toast.makeText(getApplicationContext(),
  2. "带图片的Toast",Toast.LENGTH_LONG);
  3. toast.setGravity(Gravity.CENTER,0,0);
  4. LinearLayouttoastView=(LinearLayout)toast.getView();
  5. ImageViewimageCodeProject=newImageView(getApplicationContext());
  6. imageCodeProject.setImageResource(R.drawable.icon);
  7. toastView.addView(imageCodeProject,0);
  8. toast.show();

4.完全自定义效果

代码

        
  1. LayoutInflaterinflater=getLayoutInflater();
  2. Viewlayout=inflater.inflate(R.layout.custom,
  3. (ViewGroup)findViewById(R.id.llToast));
  4. ImageViewimage=(ImageView)layout
  5. .findViewById(R.id.tvImageToast);
  6. image.setImageResource(R.drawable.icon);
  7. TextViewtitle=(TextView)layout.findViewById(R.id.tvTitleToast);
  8. title.setText("Attention");
  9. TextViewtext=(TextView)layout.findViewById(R.id.tvTextToast);
  10. text.setText("完全自定义Toast");
  11. toast=newToast(getApplicationContext());
  12. toast.setGravity(Gravity.RIGHT|Gravity.TOP,12,40);
  13. toast.setDuration(Toast.LENGTH_LONG);
  14. toast.setView(layout);
  15. toast.show();

5.其他线程

代码

        
  1. newThread(newRunnable(){
  2. publicvoidrun(){
  3. showToast();
  4. }
  5. }).start();

完整代码

1.Main,java

        
  1. packagecom.wjq.toast;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.view.Gravity;
  6. importandroid.view.LayoutInflater;
  7. importandroid.view.View;
  8. importandroid.view.ViewGroup;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.LinearLayout;
  12. importandroid.widget.TextView;
  13. importandroid.widget.Toast;
  14. publicclassMainextendsActivityimplementsOnClickListener{
  15. Handlerhandler=newHandler();
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  21. findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
  22. this);
  23. findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  24. findViewById(R.id.btnCustomToast).setOnClickListener(this);
  25. findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
  26. }
  27. publicvoidshowToast(){
  28. handler.post(newRunnable(){
  29. @Override
  30. publicvoidrun(){
  31. Toast.makeText(getApplicationContext(),"我来自其他线程!",
  32. Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. }
  36. @Override
  37. publicvoidonClick(Viewv){
  38. Toasttoast=null;
  39. switch(v.getId()){
  40. caseR.id.btnSimpleToast:
  41. Toast.makeText(getApplicationContext(),"默认Toast样式",
  42. Toast.LENGTH_SHORT).show();
  43. break;
  44. caseR.id.btnSimpleToastWithCustomPosition:
  45. toast=Toast.makeText(getApplicationContext(),
  46. "自定义位置Toast",Toast.LENGTH_LONG);
  47. toast.setGravity(Gravity.CENTER,0,0);
  48. toast.show();
  49. break;
  50. caseR.id.btnSimpleToastWithImage:
  51. toast=Toast.makeText(getApplicationContext(),
  52. "带图片的Toast",Toast.LENGTH_LONG);
  53. toast.setGravity(Gravity.CENTER,0,0);
  54. LinearLayouttoastView=(LinearLayout)toast.getView();
  55. ImageViewimageCodeProject=newImageView(getApplicationContext());
  56. imageCodeProject.setImageResource(R.drawable.icon);
  57. toastView.addView(imageCodeProject,0);
  58. toast.show();
  59. break;
  60. caseR.id.btnCustomToast:
  61. LayoutInflaterinflater=getLayoutInflater();
  62. Viewlayout=inflater.inflate(R.layout.custom,
  63. (ViewGroup)findViewById(R.id.llToast));
  64. ImageViewimage=(ImageView)layout
  65. .findViewById(R.id.tvImageToast);
  66. image.setImageResource(R.drawable.icon);
  67. TextViewtitle=(TextView)layout.findViewById(R.id.tvTitleToast);
  68. title.setText("Attention");
  69. TextViewtext=(TextView)layout.findViewById(R.id.tvTextToast);
  70. text.setText("完全自定义Toast");
  71. toast=newToast(getApplicationContext());
  72. toast.setGravity(Gravity.RIGHT|Gravity.TOP,12,40);
  73. toast.setDuration(Toast.LENGTH_LONG);
  74. toast.setView(layout);
  75. toast.show();
  76. break;
  77. caseR.id.btnRunToastFromOtherThread:
  78. newThread(newRunnable(){
  79. publicvoidrun(){
  80. showToast();
  81. }
  82. }).start();
  83. break;
  84. }
  85. }
  86. }

2.main,xml

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"android:padding="5dip"android:gravity="center">
  5. <Buttonandroid:layout_height="wrap_content"
  6. android:layout_width="fill_parent"android:id="@+id/btnSimpleToast"
  7. android:text="默认"></Button>
  8. <Buttonandroid:layout_height="wrap_content"
  9. android:layout_width="fill_parent"android:text="自定义显示位置"
  10. android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
  11. <Buttonandroid:layout_height="wrap_content"
  12. android:layout_width="fill_parent"android:id="@+id/btnSimpleToastWithImage"
  13. android:text="带图片"></Button>
  14. <Buttonandroid:layout_height="wrap_content"
  15. android:layout_width="fill_parent"android:text="完全自定义"
  16. android:id="@+id/btnCustomToast"></Button>
  17. <Buttonandroid:layout_height="wrap_content"
  18. android:layout_width="fill_parent"android:text="其他线程"
  19. android:id="@+id/btnRunToastFromOtherThread"></Button>
  20. </LinearLayout>

3.custom.xml

        
  1. <?xmlversion="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>

注意: 使用toast = new Toast(context); 一定要先 toast.setView() ,然后才能toast.Show() 不然会出错。同时,我使用

LinearLayout toastView = (LinearLayout) toast.getView(); 然后再在这个LinearLayout种添加TextView会出错,后来就直接定义一个LinearLayout了。

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)RadioButton+Viewpager+Fragment实现底部导航栏
  3. Android应用自动更新功能的实现!!!软件更新,自动下载,安装
  4. Android(安卓)textview 不使用ScrollView也可滚动的方法
  5. android文件上传示例分享(android图片上传)
  6. 【原创】android中实现底部tabhost
  7. Android(安卓)listview里面包含checkbox
  8. android 问题汇总系列之四
  9. Android开发应用异步检查更新代码

随机推荐

  1. Android review Android中的测试
  2. 基于Android(安卓)5.1系统的nfc读卡驱动
  3. Android 中WebView 视频自动播放
  4. Android直接连接数据库服务器
  5. Android(安卓)启动后台运行程序(Service)
  6. android获取各路径下的文件流
  7. Android:证书生成与应用
  8. Android中判断是否有声音在播放
  9. Android 通过AudioTrack播放CAF音频
  10. 获取Android设备电池信息