Toast调整显示位置

背景:

     Toast默认显示在界面底部,因这位置在部分界面会遮挡界面元素,故需要调整位置。

解决方案:

 1.显示在顶部

private void showToast(Context context,String text) {
        Toast toast = Toast.makeText(context,text,Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP,0,0);
        toast.show();
    }

 2.显示在中间

private void showToast(Context context,String text) {
        Toast toast = Toast.makeText(context,text,Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

3.显示在垂直方向1/3处

private void showToast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        Point size = new Point();
        windowManager.getDefaultDisplay().getSize(size);
        toast.setGravity(Gravity.TOP, 0, size.y / 3);
        toast.show();
    }

4.显示在垂直方向2/3

private void showToast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        Point size = new Point();
        windowManager.getDefaultDisplay().getSize(size);
        toast.setGravity(Gravity.BOTTOM, 0, size.y / 3);
        toast.show();
    }

 

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/82500496