本篇博客主要介绍如何使用Toast,Toast是Android提供的一个轻量级的用户提醒控件,使用也很简单,就相当一个极简的dialog!!!下面将向您介绍一些Toast的详细用法:

1、普遍使用的方法:

    Context context = getApplicationContext();    CharSequence text = "Hello toast!";    int duration = Toast.LENGTH_SHORT;    Toast toast = Toast.makeText(context, text, duration);    toast.show();

一般情况下,我们都是这样使用Toast的,就跟其他的UI一样,初始化一个UI需要传入一个Context,这里是通过getApplicationContext获取应用程序的上下文!!!

2、设置Toast显示的位置:
一般情况下,Toast显示在屏幕的下半屏幕中,就像下图所示的那样:
如何正确使用Toast进行用户提醒_第1张图片
我们可以通过代码更新Toast显示的位置:

    Context context = getApplicationContext();    CharSequence text = "Hello toast!";    int duration = Toast.LENGTH_SHORT;    Toast toast = Toast.makeText(context, text, duration);    toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);    toast.show();

方法原型:

public void setGravity(int gravity, int xOffset, int yOffset)

这里的参数意义就不介绍,相信您根据名字就可以猜出来!!!

改变位置后的Toast:
如何正确使用Toast进行用户提醒_第2张图片

3、自定义Toast的Layout:
Toast的布局如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                  android:id="@+id/custom_toast_container"                  android:orientation="horizontal"                  android:layout_width="fill_parent"                  android:layout_height="fill_parent"                  android:padding="8dp"                  android:background="#DAAA"                  >        <ImageView android:src="@drawable/droid"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:layout_marginRight="8dp"                   />        <TextView android:id="@+id/text"                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:textColor="#FFF"                  />    LinearLayout>

在代码中解析layout,并将解析的布局添加至Toast中,具体代码如下所示:

    public void onShowCustomToast(View view) {        LayoutInflater inflater = getLayoutInflater();        View layout = inflater.inflate(R.layout.toast_layout,                null);        TextView text = (TextView) layout.findViewById(R.id.text);        text.setText("This is a custom toast");        Toast toast = new Toast(getApplicationContext());        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);        toast.setDuration(Toast.LENGTH_LONG);        toast.setView(layout);        toast.show();    }

代码运行效果:
如何正确使用Toast进行用户提醒_第3张图片

好了,关于Toast到这里差不多就结束了,有兴趣的朋友可以更加深入的研究学习,在这里就不赘述了!!!

这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!

如何正确使用Toast进行用户提醒_第4张图片

代码稍后会上传至GitHub中:
代码地址:https://github.com/zhuyuqiang2017/Other

更多相关文章

  1. Android 的 dex2jar 和 jd-gui 反编译 apk 源代码
  2. Android Menu详解及示例代码
  3. 在android中利用多线程实现对控件的更新(动态修改文本框中的值)。
  4. 用HTML+JS实现Android闹钟功能,附带Alarm代码分享
  5. Android 图片显示与屏幕适配的问题
  6. Android JUnit Test——批量运行测试代码
  7. 在 Android 使用 Support Annotations 注解优化代码
  8. Android判断网络连接是否可用(代码)

随机推荐

  1. android itemtouchhelper-extension 仿微
  2. Android之activity跳转显示桌面及切换黑
  3. Android(安卓)删除指定文件代码
  4. Android(安卓)安卓使用MediaPlayer播放列
  5. Android支持展开/收缩功能的列表控件
  6. Android(安卓)CheckBox example
  7. Android实现DES对字符串加密
  8. Android之拖拉功能
  9. Android安装使用过程
  10. Android(安卓)SDK 历史版本