關於計算當前界面(activity,fragment)顯示窗口寬高getWindowVisibleDisplayFrame

當前界面的窗口大小,有導航欄時的高度,沒有導航欄時的高度,我們可以用view的getWindowVisibleDisplayFrame()來獲取

以下是我在項目中的應用

        int height;
        int[] location = new int[2];
        // 獲得位置
        mDataChart.getLocationOnScreen(location);

        Rect rect = new Rect();
        mDataChart.getWindowVisibleDisplayFrame(rect);
        height = rect.bottom - location[1];

以下計算popwindow顯示的高度問題,另一個顯示問題,記錄一下,以備後用。

  public void showPopupWindow() {
        if(!isShowing()){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Rect rect = new Rect();
                upView.getGlobalVisibleRect(rect);
                //如果直接測量anchor,會導致anchor的屬性改變,所以需要new個新view
                View view = new View(mContext);
                view.setLayoutParams(upView.getLayoutParams());
                int h = getViewHeight(view) - rect.bottom;
                setHeight(h);
            }
            showAsDropDown(upView);
        }
    }
    public void hidePopupWindow() {
        dismiss();
    }

    private int getViewHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }

 

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