Android 不同版本7.0以下,7.0,7.1以上 popwindow展示位置控制分享

今天給大家分享下Android不同版本下popwindow展示位置控制;公司的項目在進行雲測後發現了popwindow適配出現問題;

下面先給大家展示下我之前有問題代碼:

if (Build.VERSION.SDK_INT >=24) {
    // 系統7.0以上, popupwindow底層修改了Gravity屬性 Gravity.START
    // | Gravity.TOP
    int[] a = new int[2];
    anchor.getLocationOnScreen(a);
    showAtLocation(activityWeakReference.get().getWindow().getDecorView(),
            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());
} else {
    CardsListPanel.super.showAsDropDown(anchor);
}

上面代碼的意思就是在Android系統7.0以下時就調用popwindow.showAsDropDown(anchor)方法;

7.0以上的就調用:

showAtLocation(activityWeakReference.get().getWindow().getDecorView(),
            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());
設置顯示的樣式,以及popwindow展示的起始位置anchor到屏幕的高度+anchor自身的高度;

我們看下效果圖:

7.0以下和7.0的界面是正常的,可是7.1以上的就尷尬了;



7.1以上的界面:



界面上面的搜索框直接被遮蓋住了;查了百度,上面千篇一律都是說要像上面那樣寫;

搞了半天才查到可行的方法,這裏大家分享下,把代碼改成下面這樣:

if (Build.VERSION.SDK_INT >=24) {
    // 系統7.0以上, popupwindow底層修改了Gravity屬性 Gravity.START
    // | Gravity.TOP
    int[] a = new int[2];
    anchor.getLocationOnScreen(a);
    // 7.1以上 版本處理
    if (Build.VERSION.SDK_INT >= 25) {
        //note!Gets the screen height without the virtual key
        WindowManager wm = (WindowManager) getContentView().getContext().getSystemService(activity.WINDOW_SERVICE);
        int screenHeight = wm.getDefaultDisplay().getHeight();
        /*
        * PopupWindow height for match_parent,
        * will occupy the entire screen, it needs to do special treatment in Android 7.1
         */
        setHeight(screenHeight - a[1] - anchor.getHeight());
    }
    showAtLocation(activityWeakReference.get().getWindow().getDecorView(),
            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());
} else {
    CardsListPanel.super.showAsDropDown(anchor);
}
大家看到差別在哪裏了嗎?代碼意思改爲,7.0以上改成showAtLocation()方法去展示位置,但7.1以上版本要先重新設置popwindow的高度,之後再設置展示的位置;到這裏就ok了,我試了下7.1.1和8.0的手機都沒問題了;展示效果都是好的。



希望本文對大家有所幫助!!!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章