我有一个方形图像(尽管这个问题也适用于矩形图像)。我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比。图像小于 ImageView。问题是,我无法拉伸图像并“匹配”ImageView 的高度和宽度。
这是我的 XML 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"/>
<TextView android:id="@+id/name"
android:layout_below="@id/image"
android:layout_alignLeft="@id/image"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<TextView android:id="@+id/name2"
android:layout_below="@id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dp"/>
</RelativeLayout>
我使用了多种 scaleTypes: , , ,的组合fill_parent
,它们都以正确的纵横比绘制图像,但它们都没有真正放大图像和 ImageView 本身,导致 TextViews 被全部推送远离屏幕、ImageView 内的空白区域、图像未缩放或图像被裁剪。wrap_content
fitCenter
fitStart
fitEnd
centerInside
我无法完全确定正确的组合。