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

时间:2018-10-06 06:27:39

标签: java android kotlin android-view

假设我有一个类似的视图结构

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

这是简化的方法,但是我在底页中使用了上面的内容,有时我根据ConstraintLayout的高度更改CustomViewLayout的高度和底页的窥视高度。我的问题是,如果CustomViewLayout的一部分被切除,那就是-它在屏幕之外,因为ConstraintLayout不够高-我将无法获得正确的“全高”。在那种情况下,我似乎总是只能在视图的屏幕上看到可见的部分。

那么如何获得部分不在屏幕上的视图的整个高度?

谢谢!

编辑:

我应该补充一点,我尝试过的是globalLayoutListener以及customViewLayout.post {}(当然还有通常的customViewLayout.height)。但是,当视图的一部分位于屏幕外部时,这些方法都无法测量整个高度。

2 个答案:

答案 0 :(得分:2)

要检查我以前的答案,我开发了一个测试项目来检查确切的情况。当布局的某些部分位于屏幕外部时,可以成功测量布局高度(布局高度为 4231px ,其中屏幕高度为 1920px )。如果视图足够长,无法完全显示在屏幕上,则应将其置于可滚动视图中。因此,我将customViewLayout放在NestedScrollView中,以在底页扩展后消耗剩余的滚动量。这是代码和结果:

<?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"
                }
            }
        })
    }

}

屏幕:

enter image description here

答案 1 :(得分:0)

如果您要查找视图的宽度和高度,请尝试以下操作:

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