对话框在某些位置背景模糊



我试图在AlertDialog后面有一个模糊的背景。过程如下:截取活动的截图,模糊并保存为位图。然后将此位图转换为可绘制的。然后,我使用这行代码renameDialog.getWindow().setBackgroundDrawable(draw);设置警报窗口的可绘制背景问题是,即使我通过指定布局参数强制某个位置,对话框也只显示在屏幕顶部。我注意到,如果我把setBackgroundDrawable改为setBackgroundDrawableResource,并给它一个现有的绘图,那么对话框就会显示在指定的位置。

需要做什么才能使setBackgroundDrawable可以与对话框的指定位置一起使用?

谢谢!

        AlertDialog renameDialog = new AlertDialog.Builder(this).create();
        LayoutInflater factory = LayoutInflater.from(this);
        deleteDialogView = factory.inflate(R.layout.dialog_rename_playlist, null);
        //Getting Blurry screenshot to display it behind dialog
        Bitmap backgroundScreenshot = Utils.takeScreenShot(PlaylistActivity.this);
        Bitmap blurryBackground = Utils.getBlurredBitmap(backgroundScreenshot, 30);
        WindowManager.LayoutParams wmlp = renameDialog.getWindow().getAttributes();
        wmlp.gravity = Gravity.CENTER_VERTICAL;
        wmlp.x= 100;
        wmlp.y= 100;

        final Drawable draw=new BitmapDrawable(getResources(), blurryBackground);
        renameDialog.getWindow().setBackgroundDrawable(draw);
        renameDialog.getWindow().setAttributes(wmlp);
        renameDialog.setView(deleteDialogView, 0, 0, 0, 0);
        renameDialog.show();

通过将窗口的背景设置为屏幕大小的可绘制,您将使窗口增长到全屏尺寸。在这种情况下,Gravity.CENTER_VERTICAL不能做任何事情。使用Gravity.CENTER_VERTICAL计算的工作原理如下:

spacingAtTop = spacingAtBottom = (containerHeight - contentHeight) / 2

设置好背景后,contentHeight等于containerHeight

如果可能,post R.layout.dialog_rename_playlist .

有一件事你可以试试,不用做太多改变:

R.layout.dialog_rename_playlist的内容包裹在FrameLayout中,高度设置为match_parentR.layout.dialog_rename_playlist的当前父节点(称为oldParent)应该将其高度设置为wrap_content,将layout_gravity设置为center_vertical。这样至少可以把你的内容放在中心位置。但这并不能解决你的定位问题。

一个更健壮的方法是使用FrameLayout作为Activity/Fragment的父容器。在显示对话框之前,截取屏幕截图,模糊它,并将其设置为FrameLayout's 前景。让你的AlertDialog's背景透明,你应该得到同样的效果。

一堆猜测

wmlp.gravity = Gravity.CENTER_VERTICAL; //put me in the middle vertically
wmlp.x= 100; //sorry i have no effect
wmlp.y= 100;//dude!! you are wasting lines; i have no effect

do this

wmlp.gravity = Gravity.FILL; // i grow up when needed
wmlp.x= 100; // now i am useful
wmlp.y= 100; //dude!! you are wasting lines; use wmlp.y = wmlp.x = 100

希望有所帮助

我认为你可以使用PopupWindow

最新更新