美文网首页
Android ImageView高度根据图片比例自适应

Android ImageView高度根据图片比例自适应

作者: hao_developer | 来源:发表于2020-01-09 20:09 被阅读0次

设置adjustViewBounds // 是否保持宽高比

<ImageView
  android:id="@+id/iv_test"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:adjustViewBounds="true"
  android:scaleType="fitXY"
  android:layout_gravity="center"/>

重要的两句:

android:adjustViewBounds="true"
android:scaleType="fitXY"

动态设置view宽高

val imgParams = ivBg.layoutParams
        imgParams.width =if (isGone) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT
        imgParams.height =if (isGone) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT
        ivBg.layoutParams = imgParams

相关文章

网友评论

      本文标题:Android ImageView高度根据图片比例自适应

      本文链接:https://www.haomeiwen.com/subject/inluactx.html