gpt4 book ai didi

android - 如何重新测量 layout_weight?

转载 作者:行者123 更新时间:2023-11-29 01:38:03 25 4
gpt4 key购买 nike

在下面的布局 xml 中,我创建了一个带有两个 subview 的水平 LinearLayoutLinearLayout 子项应该动态拉伸(stretch)其宽度,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>

现在,我想要实现的是这样的结果:

manipulated screenshot

但我实际得到的是:

original screenshot

这是因为在我的自定义 ImageView 中,我覆盖了 onMeasure 以保持 1:1 的纵横比(简化):

override def onMeasure( widthMeasure: Int, heightMeasure: Int )
{
super.onMeasure( widthMeasure, heightMeasure )

setMeasuredDimensions( height, height )
}

现在我需要父 View 根据新的 ImageView 宽度重新计算宽度份额。我该怎么做?

最佳答案

现在我通过不调用 superonMeasure 实现解决了这个问题。它高度关注所描述的场景,在其他地方使用时可能很容易崩溃。代码也在 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 )
}
}

https://github.com/Taig/Toolbelt/blob/master/src/main/scala/com/taig/android/widget/image/AspectRatio.scala

关于android - 如何重新测量 layout_weight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com