如何在 XML 中更改 Android ImageView 的宽度以保持纵横比?

How to change the width of an Android ImageView in XML keeping aspect ratio?

我可以在 XML 中设置 Android ImageView 的宽度值而不改变其原始初始比例吗?

我在 java 中解决了这个问题,但我无法在 Android XML 中解决同样的问题。

到目前为止我尝试了什么:

int width = (inital width);
int height = (inital height);
int fixedWidth = Resources.getSystem().getDisplayMetrics().widthPixels >> 1; 
int fixedHeight = (fixedWidth * height ) / (width); // 50% width of screen
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(fixedWidth, fixedHeight);
imageView.setLayoutParams(layoutParams);

在您的 imageView 中添加属性

android:adjustViewBounds="true"

然后将您的身高包裹起来,这样您的身高就会符合纵横比