Android getWidth()在横向和纵向屏幕中返回相同的值

Android getWidth()在横向和纵向屏幕中返回相同的值,android,Android,我想使ImageView的大小基于其宽度为矩形,并通过覆盖onGlobalLayout()和放置photo.getWidth()完成。但它只适用于纵向屏幕,而不适用于横向屏幕 肖像画很好: 在横向中,图像不是矩形的 这是代码: fragment\u productdetail.xml <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:ori

我想使ImageView的大小基于其宽度为矩形,并通过覆盖
onGlobalLayout()
和放置
photo.getWidth()
完成。但它只适用于纵向屏幕,而不适用于横向屏幕

肖像画很好:

在横向中,图像不是矩形的

这是代码:

fragment\u 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>
然后我尝试放置
Log.i(“zihad”,“photo.getWidth())
inside onGlobalLayout()以获取横向宽度,然后它会在纵向中为我提供相同的宽度值。那么我应该怎么做才能使它在肖像画和风景画中发挥作用呢

*很抱歉英语不好,我在这里也要求提高我的英语水平

快速提示:

我不确定,这是否是你想要的。根据我的假设,我认为您可能需要一个带有方形ImageView的布局。如果在呈现布局后试图更改布局,则应调用
photo.requestLayout()

另一种解决方案是使用SquareImageView。要实现这一点,只需创建一个扩展ImageView的视图,并通过像这样覆盖其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()吗?你不能。在onLayout之前调用getMeasuredWidth()。所以你不能在这里调用getWidth。我恐怕getMeasuredWidth()没有给我真正的宽度。很抱歉回复晚了。getMeasuredWidth()将在应用布局之前获取宽度。因此,如果在运行时更改内容,则可能得不到真正的宽度,
public class MyImageView extends ImageView {

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