ConstraintLayout - ViewGroup 的 onMeasure()

问题描述

我创建了扩展 View自定义 ConstraintLayout。我想实现 onMeasure() 是因为我注意到在具有不同尺寸和/或填充/边距的不同组件上进行尺寸定制时存在问题。

这是我的视图布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@android:color/transparent">

    <androidx.appcompat.widget.AppCompatimageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:button="@drawable/drawable_state"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_rawRes="@raw/animation_json"
        app:lottie_speed="2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:lottie_autoplay="false"
        app:lottie_loop="false"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我想出了这样的东西:

override fun onMeasure(widthMeasureSpec: Int,heightMeasureSpec: Int) {

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        val width = when(widthMode) {
            MeasureSpec.EXACTLY -> widthSize
            MeasureSpec.AT_MOST -> min(STANDARD_WIDTH,widthSize)
            else -> STANDARD_WIDTH
        }

        val height = when(heightMode) {
            MeasureSpec.EXACTLY -> heightSize
            MeasureSpec.AT_MOST -> min(STANDARD_HEIGHT,heightSize)
            else -> STANDARD_HEIGHT
        }

        setMeasuredDimension(width,height);
    }

但事实证明,因为它是带孩子的容器,所以我也必须照顾它。所以我在这里搜索并找到了包含子视图的实现:

override fun onMeasure(widthMeasureSpec: Int,heightMeasureSpec: Int) {

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.makeMeasureSpec(widthMeasureSpec,MeasureSpec.EXACTLY)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.makeMeasureSpec(heightMeasureSpec,MeasureSpec.EXACTLY)

        for (index in 0 until childCount) {
            val child: View = getChildAt(index)
            child.measure(widthSize,heightSize)
        }

        val width = when (widthMode) {
            MeasureSpec.EXACTLY -> widthSize
            MeasureSpec.AT_MOST -> min(STANDARD_WIDTH,widthSize)
            else -> STANDARD_WIDTH
        }

        val height = when (heightMode) {
            MeasureSpec.EXACTLY -> heightSize
            MeasureSpec.AT_MOST -> min(STANDARD_HEIGHT,height)
    }

这似乎没有任何作用,但我的视图仍然不可见,因此对于它们来说,大小可能仍然为 0。 我不能说我理解这一点,而且我对布局感觉不太好。 onMeasure() 对我来说有点新,我从来没有实施过它。
如果有人能告诉我我做错了什么,我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)