0

我和这个问题有同样的问题,但那里的答案并没有解决问题。我已经子类化,并像这样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" />
4

3 回答 3

2

在 ImageView 中使用以下字段

android:scaleType="fitXY"
android:adjustViewBounds="true"
于 2014-01-20T12:47:15.590 回答
0

如此处所述,您可以在运行时获取布局的高度。获取它并将其设置为 imageview 的宽度。并且图像将变成精确的正方形。

于 2014-01-20T12:48:25.090 回答
0

无需继承“ImageView”。就这样做

<ImageView
            android:id="@+id/slider_image"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="fitXY" />
于 2014-01-20T12:40:38.473 回答