问题描述
我有一个方形图像(尽管这个问题也适用于矩形图像).我想显示尽可能大的图像,必要时拉伸它们以填充它们的父级,同时仍然保持纵横比.图像比 ImageView 小.问题是,我无法拉伸图像并匹配"ImageView 的高度和宽度.
I have a square image (though this problem also applies to rectangular images). I want to display the image as large as possible, stretching them if necessary, to fill their parents, while still maintaining the aspect ratio. The image is smaller than the ImageView. The problem is, I can't stretch the image and "match" the height and width of the ImageView.
这是我的 XML 布局文件:
This is my XML layout file:
<?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
与多个 scaleType 的组合:fitCenter
、fitStart
、fitEnd
、centerInside
和它们都以正确的纵横比绘制图像,但是它们实际上都没有按比例放大图像和 ImageView 本身,结果是在 TextViews 一直被推离屏幕、ImageView 内的空白区域、图像未缩放或图像被裁剪.
I have used many combinations of fill_parent
, wrap_content
with multiple scaleTypes: fitCenter
, fitStart
, fitEnd
, centerInside
, and they all draw the images in the right aspect ratio, but none of them actually scale the images up and the ImageView itself, resulting in either the TextViews get pushed all the way down off the screen, blank spaces inside the ImageView, image not scaled, or image cropped.
我想不出正确的组合.
推荐答案
这些:
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
应该调整图像大小并更改边界的大小以适应新的图像大小.如果它在您的设备上没有这样做,请发布您正在使用的图片以及您正在测试的设备.
should resize the image and change the size of the bounds to fit the new image size. If it does not do that on your device post the image you are using and what device you are testing on.
这篇关于ImageView 填充父的宽度或高度,但保持纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!