您的位置:首页 > 运维架构

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

2015-11-02 10:41 459 查看
相对控件位置显示:

上方显示

?

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:屏幕

来源: <http://www.xuebuyuan.com/1585492.html>

下方

?
左方

?
右方

?
来源: <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);

来源: <http://www.cnblogs.com/joey-home/archive/2012/07/02/2573855.html>

在售楼中的代码:

/**

* 显示待售和已售弹出框
*/
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();
}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: