android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题
網(wǎng)上關(guān)于android系統(tǒng)7.0的popupwindow適配的解決方案,基本都是一樣的,就是重寫PopupWindow里面的方法
但是如何進(jìn)行重寫,對于一個(gè)初次接觸的人來說,是個(gè)很頭疼的問題。一來是涉及到j(luò)ava基礎(chǔ),二來是涉及到popupwindow的源碼。
上個(gè)周我進(jìn)行了幾次嘗試,始終達(dá)不到效果。最后仔細(xì)看了popup源碼才明白。現(xiàn)在我給大家講述一下過程。
1.popup底層源碼的構(gòu)造方法問題
很多人直接進(jìn)行自定義popupwindow類,但是一使用就會(huì)報(bào)錯(cuò),popupwindw的構(gòu)造方法報(bào)錯(cuò)
查看源碼發(fā)現(xiàn),發(fā)現(xiàn)popup的構(gòu)造方法有9個(gè),
報(bào)錯(cuò)的構(gòu)造方法是這個(gè)
? ? ? ? ? ? ?通過不停地查看最后我發(fā)現(xiàn)pop的構(gòu)造方法之間是有聯(lián)系的,也就是popup的
*@paramcontentViewthe popup's content*@paramwidththe popup's width*@paramheightthe popup's height*/publicPopupWindow(View contentView, intwidth, intheight) {
this(contentView,width,height, false);}
說白了就是所有的構(gòu)造方法最后用的幾乎都是這個(gè)構(gòu)造方法,也就是我們報(bào)錯(cuò)的這個(gè)構(gòu)造方法。我們自定義popup時(shí)有兩種,一種是有明確布局的自定義,一種是沒有明確布局,但是無論是哪種,最后都是調(diào)用的是
因?yàn)槲业淖远x無需自定義布局(即在創(chuàng)建popup的時(shí)候沒有明確的布局),所以我按照源碼的方式必須重寫不需要設(shè)定布局的構(gòu)造方法,以及把方法的實(shí)現(xiàn)交給父類
/****重寫沒有設(shè)定布局的構(gòu)造方法* */publicProductPopuWindow(View contentView, intwidth, intheight) {
/****讓父類實(shí)現(xiàn),調(diào)用popup底層自己的構(gòu)造方法* */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.每次只是在點(diǎn)擊第二次的時(shí)候,才會(huì)滿足自己的效果,即第一次使用還是會(huì)遮蔽標(biāo)題欄
按照其他人的博客進(jìn)行方法重寫,重寫的方法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);}
發(fā)現(xiàn)showAsDropDown(View anchor)的實(shí)現(xiàn)是調(diào)用public void showAsDropDown(View anchor, int xoff, int yoff)
所以我們還得繼續(xù)重寫這個(gè)方法
@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);}
}
說道這里,其實(shí)看懂的人應(yīng)該問一句,是不是可以直接重寫后一個(gè)方法即可,答案是的
總結(jié)
以上是生活随笔為你收集整理的android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人民币的五种存款账户 人民币存款账户分为
- 下一篇: 用Tomcat构建一个简单图片服务器