猿问

当部分视图不在屏幕上时获取视图高度?

假设我有一个视图结构


<ConstraintLayout>

    <CustomViewLayout>

        ...

        ...

    </CustomViewLayout>

</ConstraintLayout>

这是简化的,但我在底部工作表中使用了上面的内容,有时我会ConstraintLayout根据我的CustomViewLayout身高改变底部工作表的高度和透视高度。我的问题是,如果部分CustomViewLayout被切断,也就是说 - 它有点在屏幕之外,因为ConstraintLayout它不够高 - 我不再能够获得正确的“全高”。在这种情况下,我似乎总是只能在视图的屏幕上看到可见部分。


那么我怎样才能获得部分不在屏幕上的视图的全高呢?


谢谢!


编辑:


我应该补充一点,我尝试过的是 a globalLayoutListener,以及 a customViewLayout.post {}(customViewLayout.height当然也是通常的)。但是当部分视图在屏幕之外时,这些都没有测量完整高度。


12345678_0001
浏览 120回答 2
2回答

郎朗坤

如果您正在寻找视图宽度和高度,请尝试以下操作:mCustomViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onGlobalLayout() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCustomViewLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = mCustomViewLayout.getMeasuredHeight()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = mCustomViewLayout.getMeasuredWidth()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答