ImageView的填写父母的宽度和高度,但仍保持宽高比 [英] ImageView fills parent's width OR height, but maintains aspect ratio

查看:109
本文介绍了ImageView的填写父母的宽度和高度,但仍保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

予具有正方形图像(尽管这个问题也适用于矩形图像)。我想显示的图象尽可能的大,它们拉伸如有必要,填补他们的父母,同时仍然保持高宽比。的图像比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 多种组合与多个scaleTypes: 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.

我不能完全弄清楚正确的组合这一点。

I can't quite figure the right combination for this.

推荐答案

这些:

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的填写父母的宽度和高度,但仍保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆