单击弹出窗口外面以解除它

时间:2012-09-23 18:08:46

标签: android

  

可能重复:
  How to dismiss the dialog with click on outside of the dialog?

如何在弹出窗口外点击以解除它?

这是我的代码:

cell.setOnClickListener(new OnClickListener(){

    /*This code is in a separate class so I needed to use ctx as context 
    *and the string "layout_inflater" because it was not recognizing 
    *LAYOUT_INFLATER_SERVICE*/

    @Override
    public void onClick(View arg0) {LayoutInflater layoutInflater  = 
    (LayoutInflater)ctx.getSystemService("layout_inflater");  
    View popupView = layoutInflater.inflate(R.layout.popup_window, null); 
    final PopupWindow popupWindow = new PopupWindow(popupView, 
          LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
    popupWindow.showAtLocation(newParentLayout, Gravity.CENTER, 0, 0); 
    }

我也试过添加所有这些但没有结果。

 popupWindow.setTouchable(true);
 popupWindow.setFocusable(true);
 popupWindow.setOutsideTouchable(true);
 Drawable bg = ctx.getResources().getDrawable(R.drawable.popup_bg);
 popupWindow.setBackgroundDrawable(bg);

我没有想法。有什么帮助吗?

编辑添加:主要布局是ViewPager / PagerAdapter,如果这会影响任何东西?

2 个答案:

答案 0 :(得分:10)

请将setOutsideTouchable(true)与背景一起设置。这对我来说很好。我知道将drawable背景设置为null可以杀死OnTouchListener。

答案 1 :(得分:0)

试试这种方式。

 cell.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            LayoutInflater layoutInflater  = 
                (LayoutInflater)ctx.getSystemService("layout_inflater");  
                View popupView = layoutInflater.inflate(R.layout.popup_window, null); 
                final PopupWindow popupWindow = new PopupWindow(ctx);

                popupWindow.setFocusable(true);
                popupWindow.setContentView(popupView);
                popupWindow.showAtLocation(b, Gravity.CENTER, 0, 0); 
                popupWindow.update(100, 100, 150, 150);
        }
    });
相关问题