我和这个问题有同样的问题,但那里的答案并没有解决问题。我已经子类化,并像这样ImageView
覆盖:onMeasure
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredHeight());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
这不会改变任何东西,如果我尝试使用链接帖子的答案中的代码,则ImageView
变得不可见(0 宽度/高度),因为 heightMeasureSpec 为 0,并且MeasureSpec.getSize(heightMeasureSpec)
也是0
.
目标是ImageView
与父视图一样高,并且宽度相同(因为图像是方形的)。
编辑:这是我在布局 XML 中的 ImageView:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/bottomtext"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/toptext"
android:adjustViewBounds="true"
android:padding="4dp"
android:scaleType="centerCrop" />