一、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); //控