企业网站如何优化排名,机械公司企业简介模板,网站建设管理经验,百度直播网上关于android系统7.0的popupwindow适配的解决方案#xff0c;基本都是一样的#xff0c;就是重写PopupWindow里面的方法但是如何进行重写#xff0c;对于一个初次接触的人来说#xff0c;是个很头疼的问题。一来是涉及到java基础#xff0c;二来是涉及到popupwindow的源…网上关于android系统7.0的popupwindow适配的解决方案基本都是一样的就是重写PopupWindow里面的方法但是如何进行重写对于一个初次接触的人来说是个很头疼的问题。一来是涉及到java基础二来是涉及到popupwindow的源码。上个周我进行了几次尝试始终达不到效果。最后仔细看了popup源码才明白。现在我给大家讲述一下过程。1.popup底层源码的构造方法问题很多人直接进行自定义popupwindow类但是一使用就会报错popupwindw的构造方法报错查看源码发现发现popup的构造方法有9个报错的构造方法是这个 通过不停地查看最后我发现pop的构造方法之间是有联系的也就是popup的*paramcontentViewthe popups content*paramwidththe popups width*paramheightthe popups height*/publicPopupWindow(View contentView, intwidth, intheight) {this(contentView,width,height, false);}说白了就是所有的构造方法最后用的几乎都是这个构造方法也就是我们报错的这个构造方法。我们自定义popup时有两种一种是有明确布局的自定义一种是没有明确布局但是无论是哪种最后都是调用的是因为我的自定义无需自定义布局(即在创建popup的时候没有明确的布局)所以我按照源码的方式必须重写不需要设定布局的构造方法以及把方法的实现交给父类/****重写没有设定布局的构造方法* */publicProductPopuWindow(View contentView, intwidth, intheight) {/****让父类实现调用popup底层自己的构造方法* */super(contentView,width,height, false);}publicProductPopuWindow() {super(null,0,0);}publicProductPopuWindow(View contentView) {super(contentView,0,0);}publicProductPopuWindow(intwidth, intheight) {super(null,width,height);}2.每次只是在点击第二次的时候才会满足自己的效果即第一次使用还是会遮蔽标题栏按照其他人的博客进行方法重写重写的方法public voidshowAsDropDown(View anchor)查看底层代码/*** Display the content view in a popup window anchored to the bottom-left* corner of the anchor view. If there is not enough room on screen to show* the popup in its entirety, this method tries to find a parent scroll* view to scroll. If no parent scroll view can be scrolled, the* bottom-left corner of the popup is pinned at the top left corner of the* anchor view.**paramanchorthe view on which to pin the popup window**see#dismiss()*/public voidshowAsDropDown(View anchor) {showAsDropDown(anchor,0,0);}发现showAsDropDown(View anchor)的实现是调用public void showAsDropDown(View anchor, int xoff, int yoff)所以我们还得继续重写这个方法Overridepublic voidshowAsDropDown(View anchorView, intxoff, intyoff) {if(Build.VERSION.SDK_INT Build.VERSION_CODES.N) {int[] a new int[2];anchorView.getLocationInWindow(a);showAtLocation(anchorView,Gravity.NO_GRAVITY,xoff,a[1] anchorView.getHeight() yoff);} else{super.showAsDropDown(anchorView,xoff,yoff);}}说道这里其实看懂的人应该问一句是不是可以直接重写后一个方法即可答案是的