Android獲取底部導(dǎo)航高度,修復(fù)部分機型檢測到但不顯示問題

Android獲取底部導(dǎo)航部分機型(華為nova,小米8等)出現(xiàn)了沒有底部導(dǎo)航欄,但是卻檢測到了,手動給視圖加高度的UI就出現(xiàn)了問題,下方多了一部分空白,于是查找新的方法。

檢測導(dǎo)航欄

先看原來使用的方法,Android想要獲取底部虛擬NavigationBar的高度,需要先檢測該手機有無底部導(dǎo)航,示例代碼如下:

   /**
     * 檢查是否存在虛擬按鍵欄
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private static boolean hasNavBar(Context context) {
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else {
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判斷虛擬按鍵欄是否重寫
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Exception e) {
                Timber.e(e.toString());
            }
        }
        return sNavBarOverride;
    }
獲取導(dǎo)航高度

如果返回為true才可以獲取底部導(dǎo)航的高度,下邊是獲取底部導(dǎo)航高度的代碼:

    /**
     * 獲取虛擬按鍵的高度
     */
    public static int getNavigationBarHeight(Activity activity) {
        int result = 0;
        if (hasNavBar(activity)) {
            Resources res = activity.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        Timber.d("NavigationBarHeight = " + result);
        return result;
    }

但是后邊測試過程中出現(xiàn)了問題,有些頁面橫豎屏切換動態(tài)加了這個高度,在部分手機上出現(xiàn)了,沒有底部導(dǎo)航,但是檢測到了的情況,導(dǎo)致整個布局下方出現(xiàn)了白條,于是開始找如何新的檢測底部導(dǎo)航是否存在的方法。

新方法檢測底部導(dǎo)航是否存在
    private static final String NAVIGATION = "navigationBarBackground";

    // 該方法需要在View完全被繪制出來之后調(diào)用,否則判斷不了
    public static boolean isNavigationBarExist(@NonNull Activity activity) {
        ViewGroup vp = (ViewGroup) activity.getWindow().getDecorView();
        if (vp != null) {
            for (int i = 0; i < vp.getChildCount(); i++) {
                vp.getChildAt(i).getContext().getPackageName();
                if (vp.getChildAt(i).getId() != NO_ID && NAVIGATION.equals(activity.getResources().getResourceEntryName(vp.getChildAt(i).getId()))) {
                    return true;
                }
            }
        }
        return false;
    }

這種方式需要在view加載完成后,遍歷所有view,來判斷底部導(dǎo)航是否顯示了,比上邊使用getIdentifier獲取的方式更準確,可以適配可以檢測到有,但實際沒有顯示的場景。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容