Toast Usage in Android

文档地址:Toast | Android Developers

简单介绍

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

Toast常常被用来呈现一些轻量级的信息给用户,将在显示一段时间后自动消失。比如“设置已保存”、“数字格式错误”、“复制成功”等等提示。在需要得到用户的反馈结果的时候,则应该使用Notification并重写其几个Button的Listener代码。

虽然使用简单,但是其呈现效果也是可以很丰富的。小到一段文字,大到一个完整的布局(我曾经由于传错参数直接把Activity的Layout给Toast出来了,233333)。

几种使用方法

1.使用Toast类里面的一个静态方法,这是最简单的一种使用,也比较常用。

The easiest way to use this class is to call one of the static methods that constructs everything you need and returns a new Toast object.

Toast.makeText(getApplicationContext(), "23333", Toast.LENGTH_SHORT).show();

其内部实现是这样的:

Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);

        result.mNextView = v;
        result.mDuration = duration;

        return result;

大意是使用系统内置的布局文件(android.R.*),封装了传进去的字符串,返回了一个Toast对象,直接使用show()方法就可以显示出来了。感觉这部分代码有些复杂也没关系,接下来的另外一种使用会解释一下。

2.Toast自定义View
这是Toast的根本,将一个View显示出来,从前面的实现可以看到,即使你传入的是一个字符串,也被填充到一个TextView里面的。这是Toast的基础和根本。
接下来,我们就实现一个好看一点的Bingo Toast,而仅仅是一个简单的文字。效果如下:
Toast View 演示
布局部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_toast"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@color/colorAccent"
              android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check_check"/>

    <TextView
        android:id="@+id/text"
        android:textSize="24sp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/done"/>
</LinearLayout>

代码部分

LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_toast,
                (ViewGroup)findViewById(R.id.layout_toast));

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();

inflate 填充

  • inflate这个过程可以理解为填充屏幕的过程,执行一系列的指令,将xml文件里的一个个组件和它们之间的关系填充到用户看到的屏幕上。

View inflate (int resource, ViewGroup root, boolean attachToRoot)
resource: 布局文件的id,在这里传Activity的Layout就能把当前界面直接弹出去了,哈哈哈。
root: 不是必须的。盛装inflate之后产生的控件的变量。如果需要对容器做些什么的话比较方便,比如更换整体的背景颜色。
attachToRoot: 不是很能理解,在这里直接传true。这里我们的ViewGroup和Activity的布局几乎没有联系。原文如下:Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

  • 如果直接使用xml中的原效果不加改动。直接传(int, null, false) 也是可以的。inflate这个方法还有许多重载方法。比如(int resource, ViewGroup root) 默认boolean为true。

  • Toast实例的setDuration, setView 等方法的返回值是void,所以不能像 setView(xx).setDuration(xx) 这样连写。
  • Duration 只可以使用Toast类定义的两个值:Toast.LENGTH_SHORT & Toast.LENGTH_LONG

又一篇博客完工啦,啦啦啦。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值