PopupWindow是Android中实现弹窗效果的一个类:
1.实例化一个PopupWindow
View 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();
本文介绍了Android中PopupWindow的使用,包括如何实例化、显示方式(showAtLocation和showAsDropDown)、两者区别以及如何获取焦点。PopupWindow提供了一种灵活创建弹窗效果的方法,可以在特定视图上方或下方展示。
2万+

被折叠的 条评论
为什么被折叠?



