【发布时间】:2012-06-05 06:52:40
【问题描述】:
我有一个方形图像(虽然这个问题也适用于矩形图像)。我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比。图像小于 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>
我使用了fill_parent、wrap_content 与多个比例类型的许多组合:fitCenter、fitStart、fitEnd、centerInside,它们都以正确的纵横比绘制图像,但是 它们都没有真正放大图像和 ImageView 本身,导致 TextView 被完全推离屏幕、ImageView 内的空白、图像未缩放或图像被裁剪。 p>
我无法确定正确的组合。
【问题讨论】:
-
试试这里给出的答案:stackoverflow.com/questions/5355130/…
-
@Tim:我已经设置了
adjustViewBounds=true。它以某种方式没有调整 ImageView 边界的大小。 -
你只有一张还是多张?你有没有想过使用
ScrollView?您的图像高度是否大于屏幕高度?如果是这样,您希望您的布局看起来如何?你的TextViewname,应该在图片的左边还是下面?如果您希望所有 3 个视图都适合您的屏幕,它们都应该有android:layout_height="wrap_content"。 -
@slybloty 我有多个,但我只显示一个图像。我应该更好地措辞这个问题。它正在显示一张图像。
-
你试过
android:scaleType="fitXY"吗?它会拉伸图像以匹配您指定的宽度和高度(即,它不考虑纵横比)。
标签: android android-layout android-imageview android-relativelayout