Android 關(guān)于獲取全屏模式下的寬高(隱藏狀態(tài)欄、導(dǎo)航欄,劉海屏、挖孔屏)

  • 我的需求是挖孔屏、橫屏全屏顯示狀態(tài)下獲取屏幕的寬度(即正常情況下的屏幕高度)
  • 嘗試了以下方法
方法一(無(wú)效,沒(méi)有獲取到導(dǎo)航欄的高度)
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }
方法二(無(wú)效,沒(méi)有獲取到導(dǎo)航欄的高度)
    public static int getScreenWidth1() {
        //獲取減去系統(tǒng)欄的屏幕的高度和寬度
        DisplayMetrics displayMetrics = MyApplication.getContext().getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;
        return width;
    }
方法三(無(wú)效,手機(jī)API版本等于30,待驗(yàn)證)
    public static int getRealScreenWidth() {
        int width;
        WindowManager wm = (WindowManager) MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            //獲取的是實(shí)際顯示區(qū)域指定包含系統(tǒng)裝飾的內(nèi)容的顯示部分
            width = wm.getCurrentWindowMetrics().getBounds().width();
            int height = wm.getCurrentWindowMetrics().getBounds().height();
            Insets insets = wm.getCurrentWindowMetrics().getWindowInsets()
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
            Log.e(TAG, "去掉任何系統(tǒng)欄的寬度:" + (width - insets.right - insets.left) + ",去掉任何系統(tǒng)欄的高度:" + (height - insets.bottom - insets.top));
        } else {
            //獲取減去系統(tǒng)欄的屏幕的高度和寬度
            DisplayMetrics displayMetrics = MyApplication.getContext().getResources().getDisplayMetrics();
            width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            Log.e(TAG, "width: " + width + ",height:" + height); //720,1491
        }
        return width;
    }
既然獲取到的屏幕寬度不包含導(dǎo)航欄高度,那么就自己獲取導(dǎo)航欄高度,然后加上去(在設(shè)置導(dǎo)航模式為手勢(shì)模式/導(dǎo)航鍵模式,獲取到的導(dǎo)航欄高度值是一樣的?。?!無(wú)效)
    public static int getNavigationBarHeight() {
        int height = 0;
        Context context = MyApplication.getContext();
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid != 0) {
            int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            height = context.getResources().getDimensionPixelSize(resourceId);
        }
        return height;
    }

最后發(fā)現(xiàn)這種方式是可以獲取到window的上下左右邊距,可以加以利用

        getDialog().getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
            Log.i(TAG, "onTouch touch offsetX getStableInsetBottom = " +insets.getStableInsetBottom());
            Log.i(TAG, "onTouch touch offsetX getStableInsetTop = " +insets.getStableInsetTop());
            Log.i(TAG, "onTouch touch offsetX getStableInsetLeft = " +insets.getStableInsetLeft());
            Log.i(TAG, "onTouch touch offsetX getStableInsetRight = " +insets.getStableInsetRight());
            return insets;
        });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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