设置popupWindow显示位置以及点击其他位置取消弹出
2015-11-02 10:41
459 查看
相对控件位置显示:
上方显示
?
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>
代码如下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();
}
});
}
上方显示
?
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>
下方
?
?
?
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();
}
});
}
相关文章推荐
- linux 把用户加入一个组&从这个组中移除
- squid介绍及其简单配置
- Introducing The Newly Redesigned Apache HAWQ [作者:常雷]
- OpenMP: 使用OpenMP优化程序
- 【鸟哥的linux私房菜-学习笔记】sed 工具
- Quick Start Guide for Apache Hawq[作者:盛想]
- Tomcat中文乱码问题的原理和解决方法
- tomcat
- Linux启动过程详解
- CentOS Mini 安装图像界面
- 软件维护
- linux 网卡绑定
- Linux 标准目录结构
- Apache Hawq Data Locality简介 (1) [作者:张桓]
- 大连理工大学文科实验教学中心网站和新闻学研究与方法网站
- 案例|服务化架构系统监控难题解决方案
- openstack问题解决
- apache 的访问日志被删除,apache进程异常
- Kafka学习2_apache kafka系列之在zookeeper中存储结构
- alipay支付宝 wap手机网站支付错误提示: 通用参数中少了如service、partner等必填参数