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

Android PopupWindow的使用

2015-08-03 15:04 591 查看
PopupWindow是Android中实现弹窗效果的一个类:

1.实例化一个PopupWindowView contentView = getLayoutInflater().inflate(R.layout.popwindow, null);
PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
2.PopupWindow的显示方式

popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);第一个参数是PopupWindow显示在那个view上,第二个参数是PopupWindow的显示位置,第三四个参数是PopupWindow的偏移量

另外一种显示方式是showAsDropDown(View anchor) 或showAsDropDown(View anchor, int xoff, int yoff) 

3.showAtLocation和showAsDropDown的区别

PopupWindow是设置相对某一个父view的显示方式,而showAsDropDown是设置相对于控件的向下弹出的显示方式

4.PopupWindow获取焦点

popupWindow.setFocusable(true);
5.点击PopupWindow之外的区域关闭PopupWindow
popupWindow.setBackgroundDrawable(new BitmapDrawable());
6.PopupWindow显示和消失动画
popupWindow.setAnimationStyle(R.style.animation);
7.关闭PopupWindow
popupWindow.dismiss();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: