设置popupWindow显示位置以及点击其他位置取消弹出

相对控件位置显示:

上方显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private void showPopUp(View v) {
        LinearLayout layout = new LinearLayout(this);
        layout.setBackgroundColor(Color.GRAY);
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tv.setText("I'm a pop -----------------------------!");
        tv.setTextColor(Color.WHITE);
        layout.addView(tv);
 
        popupWindow = new PopupWindow(layout,120,120);
         
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
         
        int[] location = new int[2];
        v.getLocationOnScreen(location);//得到相对当前控件的坐标位置
         
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
    }

 

getLocationInWindow和getLocationOnScreen的区别

 

// location [0]--->x坐标,location [1]--->y坐标
int[] location = new  int[2] ;
// 获取在当前窗口内的绝对坐标,getLeft , getTop, getBottom, getRight,  这一组是获取相对在它父窗口里的坐标
view.getLocationInWindow(location); 
// 获取在整个屏幕内的绝对坐标,注意这个值是要从屏幕顶端算起,也就是包括了通知栏的高度,整个屏幕的大小坐标。
view.getLocationOnScreen(location);

如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。
在onWindowFocusChanged(boolean hasFocus)中获取为好。

 

View.getLocationInWindow()和 View.getLocationOnScreen()在window占据全部screen时,返回值相同,不同的典型情况是在Dialog中时。当Dialog出现在屏幕中间时,View.getLocationOnScreen()取得的值要比View.getLocationInWindow()取得的值要大。

注:screen:屏幕
 

 

 

下方

1
popupWindow.showAsDropDown(v);

左方

1
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);

右方

1
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);

来源: <http://my.oschina.net/zhulunjun/blog/260859>

 

 

 

android:点击popupwindow以外区域 popupwindow自动消失

方法一(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等):

代码如下popupWindow.setFocusable(false);//focusable要为false(不设置默认的就是False);
//这是Activity 的OnTouchEvent。OnTouchEvent代表的是Activity 获得事件(即为PopupWindow之外)

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

if (popupWindow != null && popupWindow.isShowing()) {

popupWindow.dismiss();

popupWindow = null;

}

return super.onTouchEvent(event);

}

方法二:设置popupWindow参数(这种方法只能让自身消失,不能够提供其他伴随操作,比如让其他控件的隐藏,消失等)

pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());//关键代码
pop.setOutsideTouchable(true);


 
在售楼中的代码:
    /**
     * 显示待售和已售弹出框
     */
    private void showPopWindow() {
        int pWidthPx = Utils.dip2px(RoomInformationActivity.this, 100);//设置弹出框的宽度
        int pHeight=Utils.dip2px(RoomInformationActivity.this, 40);//设置弹出框的高度
        
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View vPopWindow = inflater.inflate(R.layout.item_popup_room, null,false);
        tv_sale = (TextView) vPopWindow.findViewById(R.id.tv_sale);
        showData();
        popWindow = new PopupWindow(vPopWindow,pWidthPx, pHeight, true);
        popWindow.setFocusable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());//重要代码
        popWindow.setOutsideTouchable(true);
        
        int[] location = new int[2];
        fl_control.getLocationOnScreen(location);
        
        popWindow.showAtLocation(fl_control, Gravity.NO_GRAVITY, location[0], location[1]-popWindow.getHeight());
        
        /**
         * 点击条目
         */
        tv_sale.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if ("待售".equals(tv_sale.getText().toString().trim())) {
                    tv_pin_control.setText("待售");
                }else {
                    tv_pin_control.setText("销控");
                }
                status=tv_pin_control.getText().toString().trim();
                popWindow.dismiss();
            }
        });
    }   

 

 

posted @ 2015-11-02 10:41  /画家/  阅读(4667)  评论(0编辑  收藏  举报