[关闭]
@coder-pig 2015-09-26T03:42:09.000000Z 字数 3211 阅读 1779

Android基础入门教程——2.5.1 Toast(吐司)的基本使用

Android基础入门教程


本节引言:

好的,终于学习完Adapter类相关的一些控件,当然除了讲解的那几个,还有其他很多的
相关的控件,就不慢慢讲解了~有需要的自行查阅文档,查看相关的用法,本节带来的是:
Android用于提示信息的一个控件——Toast(吐司)!Toast是一种很方便的消息提示框,会在
屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失!
非常常用!本节我们就来学习Toast的使用!

1.直接调用Toast类的makeText()方法创建

这是我们用的最多的一种形式了!比如点击一个按钮,然后弹出Toast,用法:
Toast.makeText(MainActivity.this, "提示的内容", Toast.LENGTH_LONG).show();
第一个是上下文对象!对二个是显示的内容!第三个是显示的时间,只有LONG和SHORT两种
会生效,即时你定义了其他的值,最后调用的还是这两个!

另外Toast是非常常用的,我们可以把这些公共的部分抽取出来,写到一个方法里!
需要显示Toast的时候直接调用这个方法就可以显示Toast,这样方便很多!
示例如下:

  1. void midToast(String str, int showTime)
  2. {
  3. Toast toast = Toast.makeText(global_context, str, showTime);
  4. toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0); //设置显示位置
  5. TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
  6. v.setTextColor(Color.YELLOW); //设置字体颜色
  7. toast.show();
  8. }

上面这个抽取出来的方法,我们发现我们可以调用setGravity设置Toast显示的位置以及获得
通过findViewById(android.R.id.message)获得显示的文本,然后进行设置颜色,或者大小等!
这就是第二种通过构造方法来定制Toast!


2.通过构造方法来定制Toast:

上面定制了文本,以及显示位置,下面我们写两个简单的例子:

1.定义一个带有图片的Toast

效果图

关键代码

  1. private void midToast(String str, int showTime)
  2. {
  3. Toast toast = Toast.makeText(mContext, str, showTime);
  4. toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //设置显示位置
  5. LinearLayout layout = (LinearLayout) toast.getView();
  6. layout.setBackgroundColor(Color.BLUE);
  7. ImageView image = new ImageView(this);
  8. image.setImageResource(R.mipmap.ic_icon_qitao);
  9. layout.addView(image, 0);
  10. TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
  11. v.setTextColor(Color.YELLOW); //设置字体颜色
  12. toast.show();
  13. }

2.Toast完全自定义

如果上面的那种还满足不了你的话,那么你完全可以自己写一个Toast的布局,然后显示出来;
但是时间我们依旧控制不了!

运行效果图

关键代码

  1. private void midToast(String str, int showTime)
  2. {
  3. LayoutInflater inflater = getLayoutInflater();
  4. View view = inflater.inflate(R.layout.view_toast_custom,
  5. (ViewGroup) findViewById(R.id.lly_toast));
  6. ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
  7. TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
  8. tv_msg.setText(str);
  9. Toast toast = new Toast(mContext);
  10. toast.setGravity(Gravity.CENTER, 0, 0);
  11. toast.setDuration(Toast.LENGTH_LONG);
  12. toast.setView(view);
  13. toast.show();
  14. }

还有自定义Toast的布局以及圆角背景:

圆角背景:bg_toast.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 设置透明背景色 -->
  4. <solid android:color="#BADB66" />
  5. <!-- 设置一个黑色边框 -->
  6. <stroke
  7. android:width="1px"
  8. android:color="#FFFFFF" />
  9. <!-- 设置四个圆角的半径 -->
  10. <corners
  11. android:bottomLeftRadius="50px"
  12. android:bottomRightRadius="50px"
  13. android:topLeftRadius="50px"
  14. android:topRightRadius="50px" />
  15. <!-- 设置一下边距,让空间大一点 -->
  16. <padding
  17. android:bottom="5dp"
  18. android:left="5dp"
  19. android:right="5dp"
  20. android:top="5dp" />
  21. </shape>

布局文件:view_toast_custom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/lly_toast"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/bg_toast"
  7. android:orientation="horizontal">
  8. <ImageView
  9. android:id="@+id/img_logo"
  10. android:layout_width="24dp"
  11. android:layout_height="24dp"
  12. android:layout_marginLeft="10dp"
  13. android:src="@mipmap/iv_lol_icon1" />
  14. <TextView
  15. android:id="@+id/tv_msg"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="10dp"
  19. android:textSize="20sp" />
  20. </LinearLayout>

非常简单,嘿嘿~


3.示例代码下载

ToastDemo.zip


本节小结:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注