简体   繁体   English

视图的一部分在屏幕外时获取视图高度?

[英]Get view height when part of the view is off screen?

Let's say I have a view structure like 假设我有一个类似的视图结构

<ConstraintLayout>
    <CustomViewLayout>
        ...
        ...
    </CustomViewLayout>
</ConstraintLayout>

This is simplified but I use the above in a bottom sheet and I sometimes change the height of the ConstraintLayout and the peek height of the bottom sheet depending on my CustomViewLayout height. 这是简化的,但我在底部工作表中使用上面的内容,我有时会根据我的CustomViewLayout高度更改ConstraintLayout的高度和底部工作表的CustomViewLayout高度。 My problem is that if part of the CustomViewLayout is cut off, that is - it is somewhat outside the screen because the ConstraintLayout isn't high enough - I'm no longer able to get a correct "full height" of it. 我的问题是,如果CustomViewLayout一部分被切断,那就是 - 它有点在屏幕之外,因为ConstraintLayout不够高 - 我不再能够获得正确的“全高”。 I always only seem to get the visible part on screen of the view in that case. 在这种情况下,我总是只看到视图屏幕上的可见部分。

So how can I get the full height of a view that is partly off screen? 那么如何才能获得部分偏离屏幕的视图的全高?

Thanks! 谢谢!

Edit: 编辑:

I should add that what I've tried is a globalLayoutListener , as well as a customViewLayout.post {} (and the usual customViewLayout.height of course). 我应该补充说,我尝试过的是一个globalLayoutListener ,以及一个customViewLayout.post {} (当然还有通常的customViewLayout.height )。 But none of these measure the full height when part of the view is outside the screen. 但是当视图的一部分位于屏幕之外时,这些都不会测量整个高度。

To check my previous answer, I developed a test project to examine the exact situation. 为了检查我之前的答案,我开发了一个测试项目来检查确切的情况。 It was successful to measure layout height when some part of it is outside of screen (layout height measured 4231px where the screen height was 1920px ). 当它的某些部分位于屏幕之外时,测量布局高度是成功的(布局高度测量为4231px ,屏幕高度为1920px )。 If the view is long enough that is not completely shown by the screen, you should put it in a scrollable view. 如果视图足够长并且屏幕未完全显示,则应将其放在可滚动视图中。 So I put the customViewLayout in a NestedScrollView to consume residual scroll amount after the bottom sheet has expanded. 因此,我将customViewLayout放在NestedScrollView以便在底部工作表展开后使用剩余滚动量。 Here is the code and result: 这是代码和结果:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layoutBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/customViewLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/colorAccent">

                <TextView
                    android:id="@+id/stateTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:layout_marginTop="16dp"
                    android:gravity="center_horizontal"/>

                <TextView
                    android:id="@+id/sizeTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:padding="24dp"
                    android:gravity="center_horizontal"/>

                <TextView
                    android:id="@+id/contentTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:padding="24dp"
                    android:text="@string/lorem"
                    android:gravity="center_horizontal"/>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

MainActivity: 主要活动:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        customViewLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                customViewLayout.viewTreeObserver.removeGlobalOnLayoutListener(this)
                val height = customViewLayout.measuredHeight
                val width = customViewLayout.measuredWidth
                sizeTextView.text = "Height: $height px                Width: $width px"
            }
        })

        val sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet)
        sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, offset: Float) {
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> stateTextView.text = "State: Hidden"
                    BottomSheetBehavior.STATE_EXPANDED -> stateTextView.text = "State: Expanded"
                    BottomSheetBehavior.STATE_COLLAPSED -> stateTextView.text = "State: Collapsed"
                    BottomSheetBehavior.STATE_DRAGGING -> stateTextView.text = "State: Dragging"
                    BottomSheetBehavior.STATE_SETTLING -> stateTextView.text = "State: Settling"
                }
            }
        })
    }

}

Screen: 屏幕:

在此输入图像描述

If you are looking for the view width and height, try this: 如果您正在寻找视图宽度和高度,请尝试以下方法:

mCustomViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mCustomViewLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            int height = mCustomViewLayout.getMeasuredHeight()
            int width = mCustomViewLayout.getMeasuredWidth()
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM