自己使用popupwindow时遇到的

来源:互联网 发布:美国新纳粹知乎 编辑:程序博客网 时间:2024/06/03 09:08

/**
02 * 初始化rightPopupWindow
03 */
04private PopupWindow initPopupWindow() {
05 
06    LayoutInflater inflater = LayoutInflater.from(this);
07    View popView = inflater.inflate(R.layout.popupwindow_content, null);
08    TextView item = (TextView) popView.findViewById(R.id.text_item);
09         
10    // 创建PopupWindow
11    final PopupWindow popupWindow = createPopupWindow(popView);
12 
13    popView.setOnTouchListener(new View.OnTouchListener() {
14        @Override
15        public boolean onTouch(View v, MotionEvent event) {
16            if (rightPop != null && rightPop.isShowing()) {
17                popupWindow.dismiss();
18            }
19            return true;
20        }
21    });
22 
23    item.setOnClickListener(new View.OnClickListener() {
24        @Override
25        public void onClick(View v) {
26            popupWindow.dismiss();
27            // ...
28        }
29    });
30         
31    return popupWindow;
32


如何在PopupWindow之外的区域点击后,PopupWindow会消失呢。 

        看下实现的相关代码:
        popupWindow.setBackgroundDrawable(new BitmapDrawable());        
       popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(button);
       ok,这样做没问题的。点击 PopupWindow之外的区域后, PopupWindow会消失 
       (1)我把第一行注释掉呢:

        //        popupWindow.setBackgroundDrawable(new BitmapDrawable());              

 popupWindow.setFocusable(true);

                popupWindow.setTouchable(true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(button); 
       这个时候会发现点击PopupWindow之外的区域后, PopupWindow不会消失。
      因此在显示popupWindow 之前我们必须要setBackgroundDrawable来进行设置哦。
      
    (2)我把setOutsideTouchable 放在showAsDropDown 后面设置呢:
                popupWindow.setBackgroundDrawable(new BitmapDrawable());              
                popupWindow.setFocusable(true);
                popupWindow.setTouchable(true);
                popupWindow.showAsDropDown(button);  
                popupWindow.setOutsideTouchable(true); 
        这个时候会发现点击 PopupWindow之外的区域后, PopupWindow会消失 。
       
    (3)然后我把setFocusable setTouchable setOutsideTouchable 放在showAsDropDown  后面呢
               popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.showAsDropDown(button);  
                popupWindow.setFocusable(true);               
                popupWindow.setTouchable(true);
                popupWindow.setOutsideTouchable(true);  

                这个时候会发现点击 PopupWindow之外的区域后, PopupWindow不会消失。




在界面使用showAsDropDown或者showAtLocation调整popupwindow的位置的时候,必须考虑popupwindow自身的宽度因素,否则在适配时popupwindow在主界面的位置难以调整

0 0
原创粉丝点击