Android 如何重新测量布局重量?

Android 如何重新测量布局重量?,android,android-layout,android-view,android-layout-weight,Android,Android Layout,Android View,Android Layout Weight,在下面的布局xml中,我创建了一个带有两个子视图的水平LinearLayout。假设LinearLayout子对象动态拉伸其宽度,则ImageView的宽度取决于父对象的高度 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> &l

在下面的布局xml中,我创建了一个带有两个子视图的水平
LinearLayout
。假设
LinearLayout
子对象动态拉伸其宽度,则
ImageView
的宽度取决于父对象的高度

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

    <LinearLayout android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:orientation="vertical">

        <!-- Some children ... -->

    </LinearLayout>

    <MyAspectRatioImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />

</LinearLayout>

现在,我需要父视图重新计算宽度共享,并记住新的ImageView宽度。我该怎么做呢?

目前,我通过一个不调用
super
onMeasure
实现解决了这个问题。它高度关注所描述的场景,在其他地方使用时可能很容易中断。代码也是Scala的,对此表示抱歉

override def onMeasure( widthMeasure: Int, heightMeasure: Int )
{
    ( Option( getDrawable ), ( getMode( widthMeasure ), getMode( heightMeasure ) ) ) match
    {
        // No Drawable
        case ( None, _ ) => setMeasuredDimensions( 0, 0 )
        // Width and height are known
        case ( _, ( EXACTLY, EXACTLY ) ) =>
        {
            // Determine which axis to adjust for the ratio
            val ( width, height ) = ratio.getDominance match
            {
                case 0 => ( getSize( widthMeasure ), ( getSize( widthMeasure ) * ratio.getValue ).toInt )
                case 1 => ( ( getSize( heightMeasure ) * ratio.getValue ).toInt, getSize( heightMeasure ) )
            }

            setMeasuredDimensions( width, height )

            // This call makes this whole thing work in first place
            if( getAdjustViewBounds )
            {
                getLayoutParams.width = width
                getLayoutParams.height = height
            }
        }
        // Only height dimension is known, adjust width to ratio
        case ( _, ( _, EXACTLY ) ) =>
        {
            val height = getSize( heightMeasure )
            setMeasuredDimensions( ( height * ratio.getValue ).toInt, height )
        }
        // Only width dimension is known, adjust height to ratio
        case ( _, ( EXACTLY, _ ) ) =>
        {
            val width = getSize( widthMeasure )
            setMeasuredDimensions( ( width * ratio.getValue ).toInt, width )
        }
        case ( Some( drawable ), ( UNSPECIFIED, UNSPECIFIED ) ) =>
        {
            val ( width, height ) = ratio.getDominance match
            {
                case 0 => ( drawable.getIntrinsicWidth, ( drawable.getIntrinsicWidth * ratio.getValue ).toInt )
                case 1 => ( ( drawable.getIntrinsicHeight * ratio.getValue ).toInt, drawable.getIntrinsicHeight )
            }

            setMeasuredDimensions( width, height )
        }
        case ( Some( drawable ), ( UNSPECIFIED, _ ) ) =>
        {
            setMeasuredDimensions(
                ( drawable.getIntrinsicWidth * ratio.getValue ).toInt,
                drawable.getIntrinsicWidth
            )
        }
        case ( Some( drawable ), ( _, UNSPECIFIED ) ) =>
        {
            setMeasuredDimensions(
                ( drawable.getIntrinsicHeight * ratio.getValue ).toInt,
                drawable.getIntrinsicHeight
            )
        }
        case _ => super.onMeasure( widthMeasure, heightMeasure )
    }
}

override def onMeasure( widthMeasure: Int, heightMeasure: Int )
{
    ( Option( getDrawable ), ( getMode( widthMeasure ), getMode( heightMeasure ) ) ) match
    {
        // No Drawable
        case ( None, _ ) => setMeasuredDimensions( 0, 0 )
        // Width and height are known
        case ( _, ( EXACTLY, EXACTLY ) ) =>
        {
            // Determine which axis to adjust for the ratio
            val ( width, height ) = ratio.getDominance match
            {
                case 0 => ( getSize( widthMeasure ), ( getSize( widthMeasure ) * ratio.getValue ).toInt )
                case 1 => ( ( getSize( heightMeasure ) * ratio.getValue ).toInt, getSize( heightMeasure ) )
            }

            setMeasuredDimensions( width, height )

            // This call makes this whole thing work in first place
            if( getAdjustViewBounds )
            {
                getLayoutParams.width = width
                getLayoutParams.height = height
            }
        }
        // Only height dimension is known, adjust width to ratio
        case ( _, ( _, EXACTLY ) ) =>
        {
            val height = getSize( heightMeasure )
            setMeasuredDimensions( ( height * ratio.getValue ).toInt, height )
        }
        // Only width dimension is known, adjust height to ratio
        case ( _, ( EXACTLY, _ ) ) =>
        {
            val width = getSize( widthMeasure )
            setMeasuredDimensions( ( width * ratio.getValue ).toInt, width )
        }
        case ( Some( drawable ), ( UNSPECIFIED, UNSPECIFIED ) ) =>
        {
            val ( width, height ) = ratio.getDominance match
            {
                case 0 => ( drawable.getIntrinsicWidth, ( drawable.getIntrinsicWidth * ratio.getValue ).toInt )
                case 1 => ( ( drawable.getIntrinsicHeight * ratio.getValue ).toInt, drawable.getIntrinsicHeight )
            }

            setMeasuredDimensions( width, height )
        }
        case ( Some( drawable ), ( UNSPECIFIED, _ ) ) =>
        {
            setMeasuredDimensions(
                ( drawable.getIntrinsicWidth * ratio.getValue ).toInt,
                drawable.getIntrinsicWidth
            )
        }
        case ( Some( drawable ), ( _, UNSPECIFIED ) ) =>
        {
            setMeasuredDimensions(
                ( drawable.getIntrinsicHeight * ratio.getValue ).toInt,
                drawable.getIntrinsicHeight
            )
        }
        case _ => super.onMeasure( widthMeasure, heightMeasure )
    }
}