android - 如何在保持纵横比的 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);
解决方案
在 imageView 中添加属性
android:adjustViewBounds="true"
并包装你的身高,这样你的身高将是纵横比
推荐阅读
- r - IRange 的排序列表对象以使所有元素减少
- r - 在闪亮的 R 中是否可以有不同类型的输出?
- python - 如何在pycharm的调试控制台中关闭代码完成?
- node.js - 请求的资源上没有“Access-Control-Allow-Origin”标头 - socket.io
- sql - 我如何将结果限制在一定数量甚至更多?
- firebase - 离子应用程序中未收到 FCM 消息数据
- ios - 如何在 iOS 模拟器上删除 hello world 的东西
- machine-learning - 机器学习噪声过滤
- ios - 删除 viewDidAppear 中的上一个子视图
- python - 将 pandas 数据框中的每日数据转换为每月数据