这是我的布局文件
<LinearLayout ...
<ImageView
android:id="@+id/feed_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:contentDescription="@string/image_content_description" />
但是 ImageView
宽度不匹配父 宽度。 (宽度为图源宽度)
Image Source loaded Lazy-loading from url.
如何缩放 ImageView 的宽度,无论图像源如何?
我要
宽度 = 匹配(或填充)父元素。
高度 = 自动缩放
最佳答案
您可以通过两种方式实现这一目标:
如果您的图像比 ImageView 大,您只能使用 xml
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" android:adjustViewBounds="true"/>
如果您的图像小于 ImageView
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:adjustViewBounds="true"/>
对于第二个选项,您必须测量图像的宽度和高度,并根据 ImageView 的实际宽度(相同比例)在代码中设置 ImageView 的高度。
关于android - 如何缩放 ImageView 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739094/