关于计算当前界面(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();
    }

 

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