如何解雇外面的PopupWindow点击

时间:2017-11-08 04:48:48

标签: java android android-popupwindow

当我在外面点击时,如何解除 PopupWindow

这是我的代码:

private void initiatePopupWindow(View viewButton) {
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popup_layout, null);
        mPopupWindow = new PopupWindow(this);
        mPopupWindow.setContentView(view);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.showAsDropDown(viewButton);
//        final PopupWindow pw = new PopupWindow(inflater.inflate(
//                R.layout.popup_layout, null, false), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
//        pw.showAtLocation(view, Gravity.BOTTOM, 0, view.getHeight());
        ListView moreOptions = (ListView) mPopupWindow.getContentView().findViewById(R.id.list_view);
        String[] options = new String[]{"Settings", "Feedback", "Contribute", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial"};

        final ArrayList<String> moreOptionsList = new ArrayList<String>();
        for (int i = 0; i < options.length; ++i) {
            moreOptionsList.add(options[i]);
        }
        ArrayAdapter<String> moreOptionsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                moreOptionsList);
        moreOptions.setAdapter(moreOptionsAdapter);
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setOutsideTouchable(true);
//        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
//                    mPopupWindow.dismiss();
//                }
//                return false;
//            }
//        });
    }

我遵循:

mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
mPopupWindow.setOutsideTouchable(true);

但它不起作用。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是我的代码中的工作,试试这个:

mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
mPopupWindow.setOutsideTouchable(true);

快乐的编码!!