android - 如何在保持宽高比的 XML 中更改 Android ImageView 的宽度?

标签 android xml imageview

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

我在 java 中解决了这个问题,但我不能在 Android XML 中做同样的事情。

Description

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

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"

然后将你的高度包裹起来,这样你的高度就会有纵横比

关于android - 如何在保持宽高比的 XML 中更改 Android ImageView 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55428550/

相关文章:

android - 在android中使用.png图像从imageView中删除多余的空格

android - 无法在 Android 中解析 ImageButton 的 id

android - LinearLayout 会干扰其他对象吗?

php - 预匹配(); - 未知修饰符 '+'

android - LaTeX 或 MathML 到 Android 上的图像

android - 从位图中获取图像并将其放入字符串中

android - 需要有关android布局设计的帮助

android - 嵌套在 DrawableContainer 中时错误拉伸(stretch) VectorDrawableCompat

java - 将 HashMap 保存到 SharedPreferences

xml - 您可以使用这种验证许可证的方式吗?