Android 獲取導航欄高度

只能在activity中使用,或者自己更改下傳入一個 不要覆蓋導航欄的window自己更改下代碼。

public int getNavigationBarHeight(Context context) {
        if (!(context instanceof Activity)) {
            return 0;
        }

        int height;
        Display display = ((Activity) context).getWindow().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getRealSize(point);

        View decorView = ((Activity) context).getWindow().getDecorView();
        Configuration conf = context.getResources().getConfiguration();
        if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) {
            View contentView = decorView.findViewById(android.R.id.content);
            height = Math.abs(point.x - contentView.getWidth());
        } else {
            Rect rect = new Rect();
            decorView.getWindowVisibleDisplayFrame(rect);
            height = Math.abs(rect.bottom - point.y);
        }
        return height;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章