Android控件系列——Popupwindowod

popupwindow

简单使用

popupWindow = new PopupWindow(View, width, height, focusable);
//参数:PopupWindow要显示的视图,宽,高,是否可点击
//宽高可设置成ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT
popupWindow.setBackgroundDrawable(new ColorDrawable());	//解决按返回键时,PopupWindow不隐藏问题
popupWindow.showAsDropDown(anchor, xoff, yoff);
//参数:指定popupWindow要显示在哪个控件的下方,X,Y轴偏移量,下、右为正值

构造方法

public PopupWindow (Context context)
public PopupWindow(View contentView)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView, int width, int height, boolean focusable)
//参数:PopupWindow要显示的视图,宽高(也可是MATCHT_PARENT和WRAP_CONTENT),是否可点击

常用方法

setContentView(contentview) 改变视图
getContentView() 获取视图
showAsDropDown(…) 显示在指定控件正下方,可设置偏移与位置
setAnimationStyle(int animationStyle) 设置动画
showAtLocation(View parent, int gravity, int x, int y)
相对于父控件位置,如正中央:Gravity.CENTER,下方:Gravity.BOTTOM,可设置偏移
setWidth(int width)
setHeight(int height)
设置宽高,popupWindow宽高属性与首层View相对应,真正想显示的View 放在第二层
setFocusable(boolean focusable)
如果PopupWindow中有Editor的话,focusable要为true。
setTouchable(boolean touchable)
setOutsideTouchable(boolean touchable)
点击PopupWindow之外的地方PopupWindow会消失
setEnterTransition(Transition enterTransition)
setExitTransition(Transition exitTransition)
setElevation(float elevation)
setIgnoreCheekPress()
setTouchInterceptor(OnTouchListener l)
setInputMethodMode(int mode)
setClippingEnabled(boolean enabled)
setSplitTouchEnabled(boolean enabled)
setAttachedInDecor(boolean enabled)
setWindowLayoutType(int layoutType)
setWindowLayoutMode(int widthSpec, int heightSpec)
setOverlapAnchor(boolean overlapAnchor)
dismiss()
update(…)
setOnDismissListener
setBackgroundDrawable(Drawable background)
设置背景,如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView,如果没有背景,则直接用contentView作为mPopupView。而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理

注意:为使代码相关性更低contentview.setOnKeyListener可用popupWindow.getContentView.setOnKeyListener代替

问题

popupWindow在一进界面就弹出,报空指针等错误

原因:根布局view还没有初始化完毕,拿不到宽和高

解决方法:延时消息,手动按钮点击

禁用返回键,强制执行popupWindow的按键

解决方法:popupWindow的setFocusable(false); ,同时在activity的onKeyDown()中判断popupWindow,如果显示,则return true;

总结

popupWindow的弹出关注点:

依附的对象:showAsDropDown()

弹出的view

位置:showAtLocation()

动画:setAnimationStyle()

猜你喜欢

转载自blog.csdn.net/mLuoya/article/details/87926852
今日推荐