日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题

發(fā)布時間:2023/12/13 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

網(wǎng)上關(guān)于android系統(tǒng)7.0的popupwindow適配的解決方案,基本都是一樣的,就是重寫PopupWindow里面的方法

但是如何進行重寫,對于一個初次接觸的人來說,是個很頭疼的問題。一來是涉及到j(luò)ava基礎(chǔ),二來是涉及到popupwindow的源碼。

上個周我進行了幾次嘗試,始終達不到效果。最后仔細看了popup源碼才明白。現(xiàn)在我給大家講述一下過程。

1.popup底層源碼的構(gòu)造方法問題

很多人直接進行自定義popupwindow類,但是一使用就會報錯,popupwindw的構(gòu)造方法報錯

查看源碼發(fā)現(xiàn),發(fā)現(xiàn)popup的構(gòu)造方法有9個,

報錯的構(gòu)造方法是這個

? ? ? ? ? ? ?通過不停地查看最后我發(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òu)造方法,也就是我們報錯的這個構(gòu)造方法。我們自定義popup時有兩種,一種是有明確布局的自定義,一種是沒有明確布局,但是無論是哪種,最后都是調(diào)用的是

因為我的自定義無需自定義布局(即在創(chuàng)建popup的時候沒有明確的布局),所以我按照源碼的方式必須重寫不需要設(shè)定布局的構(gòu)造方法,以及把方法的實現(xiàn)交給父類

/****重寫沒有設(shè)定布局的構(gòu)造方法* */publicProductPopuWindow(View contentView, intwidth, intheight) {

/****讓父類實現(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.每次只是在點擊第二次的時候,才會滿足自己的效果,即第一次使用還是會遮蔽標題欄

按照其他人的博客進行方法重寫,重寫的方法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)的實現(xiàn)是調(diào)用public void showAsDropDown(View anchor, int xoff, int yoff)

所以我們還得繼續(xù)重寫這個方法

@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);}

}

說道這里,其實看懂的人應(yīng)該問一句,是不是可以直接重寫后一個方法即可,答案是的

總結(jié)

以上是生活随笔為你收集整理的android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。