让popupwindow显示在view的上方并与该view水平居中对齐
2016-05-25 16:20
387 查看
/** 相对于listen这个view*/
private void showPopupWindow(View listen) {
PopupWindow popupWindow;
View contentView = LayoutInflater.from(HomepageActivity.this).inflate(
R.layout.realtime_order_pop, null);
// 设置关闭按钮的点击事件
Button button = (Button) contentView.findViewById(R.id.order_pop_close);
button.setOnClickListener(this);//if(popupWindow != null){popupWindow.dismiss();}
popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.order_pop_bg));
popupWindow.setOutsideTouchable(false); // 如果已经.setBackgroundDrawable()则本句不起作用
popupWindow.getContentView().measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int popupWidth = popupWindow.getContentView().getMeasuredWidth();
int popupHeight = popupWindow.getContentView().getMeasuredHeight();
// 设置好参数之后再show
int[] location = new int[2];
listen.getLocationOnScreen(location);
popupWindow.showAtLocation(listen, Gravity.NO_GRAVITY, (location[0]+listen.getWidth()/2)-popupWidth/2 , location[1]-popupHeight);
}
《!注意 布局文件 最外面不要用framelayouut 否则获取宽度可能有问题
private void showPopupWindow(View listen) {
PopupWindow popupWindow;
View contentView = LayoutInflater.from(HomepageActivity.this).inflate(
R.layout.realtime_order_pop, null);
// 设置关闭按钮的点击事件
Button button = (Button) contentView.findViewById(R.id.order_pop_close);
button.setOnClickListener(this);//if(popupWindow != null){popupWindow.dismiss();}
popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.order_pop_bg));
popupWindow.setOutsideTouchable(false); // 如果已经.setBackgroundDrawable()则本句不起作用
popupWindow.getContentView().measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int popupWidth = popupWindow.getContentView().getMeasuredWidth();
int popupHeight = popupWindow.getContentView().getMeasuredHeight();
// 设置好参数之后再show
int[] location = new int[2];
listen.getLocationOnScreen(location);
popupWindow.showAtLocation(listen, Gravity.NO_GRAVITY, (location[0]+listen.getWidth()/2)-popupWidth/2 , location[1]-popupHeight);
}
《!注意 布局文件 最外面不要用framelayouut 否则获取宽度可能有问题
相关文章推荐
- 20135337——linux实践三:ELF文件格式分析(32位系统)
- 查看linux版本的三种常用方法
- CentOS利用inotify+rsync实现文件同步
- 【OpenStack】源码级深入了解删除虚拟机操作
- linux下top命令的部分使用
- PhotoShop简单制作漂亮的格子背景
- Linux系统中给用户添加sudo权限
- org.apache.hadoop.hbase.TableExistsException: hbase:namespace
- hadoop运行作业的脚本解析
- 好网站留备
- Apache和tomcat集群搭建 session共享
- 企业系统的性能与扩展
- 一部Web应用自动化部署的进化史[AWS]-使用shell实现CodeDeploy
- OpenSSL详解
- 每天一个linux命令-ln
- Device Tree(一):背景介绍
- 谈Find指令
- docker,选择存储驱动器
- linux下SVN的搭建
- tomcat配置环境启动闪屏