-
短时间显示Toast【居下】
-
@param msg 显示的内容-字符串
*/
public static void showShortToast(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
//1、setGravity方法必须放到这里,否则会出现toast始终按照第一次显示的位置进行显示(比如第一次是在底部显示,那么即使设置setGravity在中间,也不管用)
//2、虽然默认是在底部显示,但是,因为这个工具类实现了中间显示,所以需要还原,还原方式如下:
toast.setGravity(Gravity.BOTTOM, 0, dip2px(context, 64));
toast.show();
}
/**
-
短时间显示Toast【居中】
-
@param msg 显示的内容-字符串
*/
public static void showShortToastCenter(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
-
短时间显示Toast【居上】
-
@param msg 显示的内容-字符串
*/
public static void showShortToastTop(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
/**
-
长时间显示Toast【居下】
-
@param msg 显示的内容-字符串
*/
public static void showLongToast(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.BOTTOM, 0, dip2px(context, 64));
toast.show();
}
/**
-
长时间显示Toast【居中】
-
@param msg 显示的内容-字符串
*/
public static void showLongToastCenter(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
-
长时间显示Toast【居上】
-
@param msg 显示的内容-字符串
*/
public static void showLongToastTop(Context context, String msg) {
if (toast == null) {
写在最后
对程序员来说,很多技术的学习都是“防御性”的。也就是说,我们是在为未来学习。我们学习新技术的目的,或是为了在新项目中应用,或仅仅是为了将来的面试。但不管怎样,一定不能“止步不前”,不能荒废掉。
![
![[]](https://upload-images.jianshu.io/upload_images/22459598-3e1bbd9b84cc0ef9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
文章以下内容会给出阿里与美团的面试题(答案+解析)、面试题库、Java核心知识点梳理等,需要这些文档资料的,直接点击我的GitHub免费领取~