我正在使用以下代码脚本为Android PopupWindow做暗淡背景。除了版本6(Marshmallow)之外,它适用于Android版本。
抛出“无法将FrameLayout.LayoutParams强制转换为WindowManager.LayoutParams”
如何解决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);
答案 0 :(得分:1)
您可以使用自定义对话框而不是弹出窗口。弹出窗口在android 6中创建问题。您可以在自定义对话框中执行弹出窗口等所有操作。 只需创建一个布局并膨胀到对话框中,您将获得dailog的所有属性。这是代码。
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
沿着这种外在的触摸不能解雇它。你无法在android 6的弹出窗口中看到这个。
答案 1 :(得分:0)
我也面临同样的问题。对于Marshmallow及以上版本,您需要再添加一个getParent()来访问容器。请使用以下代码:
if (android.os.Build.VERSION.SDK_INT > 22) {
contentView = (View) pwindow.getContentView().getParent().getParent();
} else {
contentView = (View) pwindow.getContentView().getParent();
}
答案 2 :(得分:0)
这适用于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);
}
}
当您在弹出窗口中调用true
时,您基本上需要使用showAsDropDown
调用它;当弹出窗口被解除时(可能是通过false
)<{1}} / p>