設置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();
            }
        });
    }   

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM