获取视图的可见高度

Che*_*rot 3 android android-activity

状态栏打开后,是否可以知道我的活动的可见高度?

我想知道屏幕的可见高度。

Adi*_*ikh 5

试试View treeobserver方法。

   main_layout.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() 
                    {

                        Rect r = new Rect();
                        // r will be populated with the coordinates of your view
                        // that area still visible.
                        main_layout.getWindowVisibleDisplayFrame(r);

                        int heightDiff = main_layout.getRootView().getHeight()-(r.bottom -r.top);

                    }
    });
Run Code Online (Sandbox Code Playgroud)

将main_layout替换为所需的View对象。希望能有所帮助。