简体   繁体   English

Android 6 中的 PopupWindow 暗淡背景(棉花糖)

[英]PopupWindow Dim Background in Android 6 (Marshmallow)

I am using the following code script to do dim background for Android PopupWindow.It is working well for the android version except version 6 (Marshmallow).我正在使用以下代码脚本为 Android PopupWindow 做暗淡的背景。它适用于除版本 6 (Marshmallow) 之外的 android 版本。

It throws "Cannot cast FrameLayout.LayoutParams to WindowManager.LayoutParams"它抛出“无法将 FrameLayout.LayoutParams 转换为 WindowManager.LayoutParams”

How to solve this problem for Android version 6.I dont want to use Dialog. Android 6 版本如何解决这个问题。我不想使用 Dialog。

PopupWindow popup = new PopupWindow(contentView, width, height);
popup.setBackgroundDrawable(null);
popup.showAsDropDown(anchor);

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) contentView.getLayoutParams();
p.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.3f;
wm.updateViewLayout(contentView, p);

you can use custome dialog instead of popup window.您可以使用自定义对话框而不是弹出窗口。 popup window create problem in android 6. You can do everything like popup window in custom dialog. android 6 中的弹出窗口创建问题。您可以在自定义对话框中执行诸如弹出窗口之类的所有操作。 just create a layout and inflate into dialog and you will get every property of dailog.只需创建一个布局并膨胀到对话框中,您将获得 dailog 的每个属性。 here is code.这是代码。

 final Dialog dialog = new Dialog(context);
  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //for remove default title
    dialog.setCancelable(false); //for canceal back button click 
    dialog.setContentView(R.layout.payment_popup_window);

    ImageButton variable_name=(ImageButton)dialog.findViewById(R.id.xml_id);
    ImageButton variable_name2=(ImageButton)dialog.findViewById(R.id.xml_id2);
    Button variable_name3=(Button)dialog.findViewById(R.id.xml_id3);
    variable_name1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //your code
            dialog.dismiss();
        }
    });

   variable_name2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your code
            dialog.dismiss();
        }
    });

    variable_name3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
    Window window = dialog.getWindow();
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //for making dialog fit in android screen

Along this outside touch can't dismiss it.沿着这种外部接触不能忽视它。 you can't get this in popup window in android 6.你不能在 android 6 的弹出窗口中得到这个。

I also faced the same issue.我也面临同样的问题。 For Marshmallow and above, you need to add one more getParent() to access the container.对于 Marshmallow 及以上,您需要再添加一个 getParent() 来访问容器。 Use the code below:使用下面的代码:

if (android.os.Build.VERSION.SDK_INT > 22) {
    contentView = (View) pwindow.getContentView().getParent().getParent();
} else {
    contentView = (View) pwindow.getContentView().getParent();
}

This works for me on Android M and above:这适用于我在 Android M 及更高版本上:

public static void controlPopupDim(PopupWindow popupWindow, boolean showing) {
    View parent = popupWindow.getContentView().getRootView();
    Context context = parent.getContext();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams wlp = (WindowManager.LayoutParams) parent.getLayoutParams();
    wlp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    wlp.dimAmount = showing ? 0.5f : 0f;
    if (wm != null) {
        wm.updateViewLayout(parent, wlp);
    }
}

You basically need to call it with true when you call the showAsDropDown on your popup and false when the popup is dismissed (may be via the onDismissListener )当您在弹出窗口中调用showAsDropDown时,您基本上需要使用true调用它, showAsDropDown在弹出窗口showAsDropDown时调用false (可能通过onDismissListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM