首先是吐司在中间现显示的:
public final class CenterToast {
private CenterToast() {
}
public static void ShortCenter(Context context, String message){
toastCenter(context, android.widget.Toast.LENGTH_SHORT, message);
}
public static void ShortCenter(Context context, int resId){
toastCenter(context, android.widget.Toast.LENGTH_SHORT, resId);
}
public static void LongCenter(Context context, String message){
toastCenter(context, android.widget.Toast.LENGTH_LONG, message);
}
public static void LongCenter(Context context, int resId){
toastCenter(context, android.widget.Toast.LENGTH_LONG, resId);
}
private static android.widget.Toast toast;
private static void toastCenter(Context context, int duration, String message, Object... args){
if (args.length > 0) {
message = String.format(message, args);
}
toast = android.widget.Toast.makeText(context, message, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
private static void toastCenter(Context context, int duration, int resId, Object... args) {
String message = context.getString(resId);
if (args.length > 0) {
message = String.format(message, args);
}
toast = android.widget.Toast.makeText(context, message, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
public static void Long(Context context, int resId) {
toast(context, android.widget.Toast.LENGTH_LONG, resId);
}
public static void Long(Context context, int resId, Object... args) {
toast(context, android.widget.Toast.LENGTH_LONG, resId, args);
}
public static void Long(Context context, String message) {
toast(context, android.widget.Toast.LENGTH_LONG, message);
}
public static void Long(Context context, String message, Object... args) {
toast(context, android.widget.Toast.LENGTH_LONG, message, args);
}
public static void Short(Context context, int resId) {
toast(context, android.widget.Toast.LENGTH_SHORT, resId);
}
public static void Short(Context context, int resId, Object... args) {
toast(context, android.widget.Toast.LENGTH_SHORT, resId, args);
}
public static void Short(Context context, String message) {
toast(context, android.widget.Toast.LENGTH_SHORT, message);
}
toast(context, android.widget.Toast.LENGTH_SHORT, message,args);
}
private static void toast(Context context, int duration, int resId, Object... args) {
String message = context.getString(resId);
if (args.length > 0) {
message = String.format(message, args);
}
android.widget.Toast.makeText(context, message, duration).show();
}
private static void toast(Context context, int duration, String message, Object... args) {
if (args.length > 0) {
message = String.format(message, args);
}
android.widget.Toast.makeText(context, message, duration).show();
}