想要动态大小的ImageView Android吗

想要动态大小的ImageView Android吗,android,android-imageview,android-bitmap,Android,Android Imageview,Android Bitmap,我想要一个ImageView,当高度大于宽度时,它将高度设置为340dp,宽度设置为wrap\u content,当宽度大于高度时,它将高度设置为wrap\u content,宽度设置为400dp 我想通过保持纵横比来实现所有这些 提前谢谢 您可以通过指定 android:layout\u width和android:layout\u height 要将图像的纵横比加载到ImageView的布局边界内部,可以将android:scaleType设置为fitCenter 因此,layout.xml

我想要一个
ImageView
,当高度大于宽度时,它将高度设置为
340dp
,宽度设置为
wrap\u content
,当宽度大于高度时,它将高度设置为
wrap\u content
,宽度设置为
400dp

我想通过保持纵横比来实现所有这些

提前谢谢

  • 您可以通过指定
    android:layout\u width
    android:layout\u height
  • 要将图像的纵横比加载到
    ImageView
    的布局边界内部,可以将
    android:scaleType
    设置为
    fitCenter
  • 因此,
    layout.xml
    中的
    ImageView
    可以定义如下:

    <ImageView
            android:scaleType="fitCenter"
            android:layout_width="400dp"
            android:layout_height="340dp"
            (...)
            />