您的位置:首页 > 移动开发 > Android开发

Android在Dialog中显示PopupWindow不全问题解决

2014-05-13 11:10 585 查看

问题:

使用PopupWindow的showAtLocation(View parent,Gravity g,int x ,int y)方法来指定它的位置。

如果parent参数传Dialog上的控件,PopupWindow显示是在Dialog之上,但是他的显示位置会因为Dialog的大小而载掉一部分;
如果parent参数传Window.getDecorView(),g,x和y的值不变,PopupWindow是在屏幕下方弹出,但是会被Dialog层挡住。

网上解决方法如下:

1、实例化PopupWindow时,设置其width为屏幕宽度;

2、获取PopupWindow的高度:

popup.getContentView().measure(0, 0);

int height = popup.getContentView().getMeasuredHeight();

3、调用showAtLocation方法时,第一个参数传Dialog上的View,比如edit,调用方法如下:

showAtLocation(edit,Gravity.BOTTOM,0,-height);

这样弹出PopupWindow就会在屏幕下方,并且悬浮在Dialog之上而不会被Dialog遮挡。

我的解决方法:

设置dialog背景透明的风格,并把dilog大小设置为全屏,dilog的内容居中,再设置PopupWindow即可!
设置Dialog全屏 代码:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = getContext().getResources().getDisplayMetrics().widthPixels;
lp.height = getContext().getResources().getDisplayMetrics().heightPixels; // 如果不是全屏要送去状态栏高度
getWindow().setAttributes(lp);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐