如何使用 layout_height="0dp" 获得实际高度布局?

How to get real height layout with layout_height="0dp"?

我需要根据FrameLayout android:id="@+id/heart_strength_backgroundheight来设置FrameLayout android:id="@+id/heart_strength的高度,它的高度是这样设置的:

            <FrameLayout
                android:id="@+id/cardiogram_background_light"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="@dimen/default_screen_margin"
                android:layout_marginTop="@dimen/chart_widget_margin_top"
                android:layout_marginEnd="@dimen/default_screen_margin"
                android:background="@drawable/chart_widget_background_light_gray"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.184"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/heart_rate_diagram_background_light" />

            <FrameLayout
                android:id="@+id/heart_strength_background"
                android:layout_width="@dimen/cardiogram_status_bar_width"
                android:layout_height="0dp"
                android:layout_marginEnd="@dimen/default_screen_margin"
                android:background="@drawable/chart_widget_background_dark_gray"
                app:layout_constraintBottom_toBottomOf="@+id/cardiogram_background_light"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/cardiogram_background_light" >

                <FrameLayout
                    android:id="@+id/heart_strength"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:heartStrength="@{viewmodel.heartStrengthLiveData}"
                    android:background="@drawable/chart_widget_background_light_gray"
                    android:backgroundTint="@color/turquoise"
                    android:layout_gravity="bottom"/>
            </FrameLayout>

当我尝试获取 heart_strength.parent 布局的实际高度时:

    @JvmStatic
    @BindingAdapter("app:heartStrength")
    fun setHeartStrengthViewHeight(bar: FrameLayout, level: Int) {
        val barParent = bar.parent as FrameLayout
        println("bar parent height: ${barParent.layoutParams.height}")
    }

我得到 0。怎么知道实际身高?

我有一张卡 (cardiogram_background_light)。它的高度以 % 为单位动态变化。因此,位于此卡片中的栏的最大高度也会动态变化。之前,我将其高度设置为 maxHeight * Value /100。但是现在 maxHeight 在不同的屏幕尺寸上动态变化,我想知道它的值。

因为调用setHeartStrengthViewHeight时视图还没有绘制。要解决此问题,请尝试以下操作:

@JvmStatic
@BindingAdapter("app:heartStrength")
fun setHeartStrengthViewHeight(bar: FrameLayout, level: Int) {
    val barParent = bar.parent as FrameLayout

    val observer : ViewTreeObserver = barParent.viewTreeObserver
    observer.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            println("bar parent height: ${barParent.height}")
            barParent.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })

}