【问题标题】:How to change the width of an Android ImageView in XML keeping aspect ratio?如何在保持纵横比的 XML 中更改 Android ImageView 的宽度?
【发布时间】:2019-03-30 05:41:38
【问题描述】:

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

【问题讨论】:

    标签: android xml imageview


    【解决方案1】:

    在你的 imageView 中添加属性

    android:adjustViewBounds="true"
    

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

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      相关资源
      最近更新 更多