首页 > 解决方案 > 如何在保持纵横比的 XML 中更改 Android ImageView 的宽度?

问题描述

我可以在 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);

标签: androidxmlimageview

解决方案


在 imageView 中添加属性

android:adjustViewBounds="true"

并包装你的身高,这样你的身高将是纵横比


推荐阅读