【问题标题】:Dialog with blurry background at certain position特定位置背景模糊的对话框
【发布时间】:2015-10-06 15:29:53
【问题描述】:

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

需要做什么才能使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();

【问题讨论】:

  • 您是否尝试过将背景设置为对话框的最后一个操作?
  • 试试这个库:Blur Dialog Fragment
  • @wasyl 是的,我做到了,但没用。我相信问题也与风格有关。
  • @vipul_asri 谢谢,但我不想为此使用库。

标签: android android-alertdialog blur layoutparams


【解决方案1】:

通过将窗口的背景设置为屏幕大小的可绘制对象,您可以使窗口增长到全屏尺寸。在这种情况下,Gravity.CENTER_VERTICAL 不能做任何事情。使用Gravity.CENTER_VERTICAL 进行计算是这样的:

spacingAtTop = spacingAtBottom = (containerHeight - contentHeight) / 2

设置背景后contentHeight等于containerHeight

如果可能,请发帖R.layout.dialog_rename_playlist

一件事你可以在不做太多改变的情况下尝试:

R.layout.dialog_rename_playlist 的内容包装在FrameLayout 中,高度设置为match_parentR.layout.dialog_rename_playlist 的当前父级(称为 oldParent)应将其高度设置为 wrap_contentlayout_gravity 设置为 center_vertical。这至少应该将您的内容带到中心。但它不会解决您的定位问题。

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

【讨论】:

    【解决方案2】:

    一堆猜测

    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
    

    这样做

    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
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      我觉得你可以用PopupWindow

      【讨论】:

        猜你喜欢
        • 2018-01-29
        • 1970-01-01
        • 2023-01-13
        • 2019-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        相关资源
        最近更新 更多