Toast的使用

public class QyToast {

    private static Toast mToast;

    /**
     * 仅显示最后一次的toast
     * @param msg
     */
    public static void showToast(String msg){
        if(mToast != null){
            mToast.cancel();
        }
        mToast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 居中 显示
     * @param msg
     */
    public static void showToast2(String msg){
        Toast toast = Toast.makeText(getApplicationContext(), "Normarl toast", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    /**
     * 自定义布局
     * @param msg
     * @param context
     */
    public static void showToast3(String msg, Activity context){
        LayoutInflater inflater = context.getLayoutInflater();
        View view = inflater.inflate(R.layout.toast_layout, null);
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }


}

 

你可能感兴趣的:(android)