android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题
網上關于android系統7.0的popupwindow適配的解決方案,基本都是一樣的,就是重寫PopupWindow里面的方法
但是如何進行重寫,對于一個初次接觸的人來說,是個很頭疼的問題。一來是涉及到java基礎,二來是涉及到popupwindow的源碼。
上個周我進行了幾次嘗試,始終達不到效果。最后仔細看了popup源碼才明白?,F在我給大家講述一下過程。
1.popup底層源碼的構造方法問題
很多人直接進行自定義popupwindow類,但是一使用就會報錯,popupwindw的構造方法報錯
查看源碼發現,發現popup的構造方法有9個,
報錯的構造方法是這個
? ? ? ? ? ? ?通過不停地查看最后我發現pop的構造方法之間是有聯系的,也就是popup的
*@paramcontentViewthe popup's content*@paramwidththe popup's width*@paramheightthe popup's 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);}
}
說道這里,其實看懂的人應該問一句,是不是可以直接重寫后一個方法即可,答案是的
總結
以上是生活随笔為你收集整理的android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人民币的五种存款账户 人民币存款账户分为
- 下一篇: 银行国际结算方式