ImageView一维适合自由空间,第二维评估保持纵横比

ATo*_*Tom 14 android android-layout android-imageview

我需要像<img width="100%" />Android 这样的东西<ImageView>.我的意思是调整所有可用空间的宽度(缩小或放大图像的宽度)并自动更改高度以保持纵横比.

Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">

使用android:scaleType="fitCenter"图像我想要的,但只使用图像,不会改变视图本身的高度.当我将booth尺寸设置为match_parent时,我会得到我想要的东西,但前提是我在屏幕上只有一个图像.但我需要一些图像下的文字.

这是创建ImageView的孩子的唯一方法吗?例如android:adjustViewBounds="true",我没有做任何事情.

ATo*_*Tom 18

我发现问题出在哪里.ImageView的默认实现无法放大图像.如果图像大于屏幕宽度android:adjustViewBounds ="true"正常工作并调整高度以保持图像的宽高比.但如果图像小于屏幕宽度,则不会将其放大.

这是由此代码引起的

if (newHeight <= heightSize) {
    heightSize = newHeight;
} 
Run Code Online (Sandbox Code Playgroud)

在ImageView类中(在第688行,API 10)

我做了ImageView的特殊孩子,允许放大图像以适应宽度.

这是:https://gist.github.com/tprochazka/5486822