layout中设置图片自适应大小,并且设置最大宽高

本文介绍了一种在Android布局中使图片自适应显示并限制其最大尺寸的方法。通过使用ImageView的特定属性,如adjustViewBounds、maxWidth及maxHeight等,可以确保图片既保持原始比例又不超过指定的最大尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ayout中设置图片自适应大小,并且设置最大宽高,当图片的宽高大于设置的最大值时,宽高值为设置的最大值。

Java代码 收藏代码
  1. <ImageViewandroid:id="@+id/image_view"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:adjustViewBounds="true"
  5. android:maxWidth="42dp"
  6. android:maxHeight="42dp"
  7. android:scaleType="fitCenter"
  8. android:layout_marginLeft="3dp"
  9. android:src="@drawable/icon"
  10. />

关键代码:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="42dp"
android:maxHeight="42dp"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值