Android自定义组件系列【17】——教你如何高仿微信录音Toast

本文介绍了Android开发中如何自定义Toast,包括如何改变Toast的显示位置和内容,探讨了为何需要延长Toast显示时长,并详细讲解了如何通过CountDownTimer实现显示时间超过3s的Toast,以达到类似微信录音时的Toast效果。
摘要由CSDN通过智能技术生成

一、Toast介绍

平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.
When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

Toast最基本的用法很简单,不用说大家都会(这里切记要调用show()去显示)

public static void showToast(Context context){
    Toast.makeText(context, "欢迎关注水寒的CSDN博客", Toast.LENGTH_LONG).show();
}

这里写图片描述

二、自定义Toast

上面的Toast一般显示在手机偏下的一个位置上,有的时候我们需要将这个Toast的位置或者内容进行修改,比如让显示在屏幕中间,让内容里面有图片,这样的修改也比较容易,Toast有一个和ActionBar类似的方法setView(),这个方法就是提供给我们自定义Toast的。

这里写图片描述

public static void showToast(Context context){
    LayoutInflater inflater = LayoutInflater.from(context);
    View toastView = inflater.inflate(R.layout.toast_test_custome, null);
    Toast toast = new Toast(context); 
    toast.setDuration(3000);
    toast.setView(toastView);                //设置自定义view
    toast.setGravity(Gravity.CENTER, 0, 0);  //控
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值