ImageView.getWidth()在横屏和竖屏屏幕中返回相同的值。

3
我希望根据ImageView的宽度使其大小成为矩形,这可以通过重写onGlobalLayout()并使用photo.getWidth()来完成。但是,它只在竖屏屏幕上工作,在横屏屏幕上不起作用。 竖屏正常工作:

enter image description here

在横向模式下,图像不是矩形形状。

enter image description here

这是代码:

fragment_productdetail.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/ivImage0"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
</LinearLayout>

FragmentProductDetail.java

ivPhotos[0] = (ImageView) inflatedView.findViewById(R.id.ivImage0);
ivPhotos[1] = (ImageView) inflatedView.findViewById(R.id.ivImage1);
ivPhotos[2] = (ImageView) inflatedView.findViewById(R.id.ivImage2);
ivPhotos[3] = (ImageView) inflatedView.findViewById(R.id.ivImage3);

for (final ImageView photo : ivPhotos) {
    photo.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            photo.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            photo.getLayoutParams().height = photo.getWidth();
        }
    });
}

然后我尝试将Log.i("zihad", ""+photo.getWidth());放在onGlobalLayout()里以获取横屏宽度,但是它给我提供了相同的纵向宽度。那么我该怎么做才能使它在纵向和横向都起作用呢?

*很抱歉我的英语不好,我也在这里提问以改善我的英语。


抱歉,但您能提供您想要实现的内容以及已渲染的内容的视觉表示吗?这可以帮助我编辑问题并为您提供答案。 - Ashik Vetrivelu
啊,抱歉,我忘了。 - zihadrizkyef
我已经编辑过了 :D - zihadrizkyef
1个回答

1

快速提示:

我不确定这是否是您想要的。根据我的理解,我认为您可能需要一个带有正方形ImageView的布局。如果您尝试在呈现后更改布局,则应在设置其高度之前调用photo.requestLayout();

另一种解决方案是使用SquareImageView。为此,只需创建一个扩展ImageView的View,并通过覆盖其onMeasure方法来设置其高度,如下所示。

public class MyImageView extends ImageView {

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int width = getMeasuredWidth();
  setMeasuredDimension(width, width);
}

现在在您的布局中,用完整包名的ImageView替换MyImageView。


我可以使用getWidth()代替getMeasuredWidth()吗? - zihadrizkyef
你不能这样做。getMeasuredWidth()在onLayout之前被调用,所以你不能在这里调用getWidth。 - Ashik Vetrivelu
我担心getMeasuredWidth()不能给我真实的宽度。 - zihadrizkyef
抱歉回复晚了。getMeasuredWidth() 将在应用布局之前获取宽度。因此,如果在运行时更改内容,则可能无法获得真实的宽度。 - Ashik Vetrivelu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接