Android:如何调整图片大小以填充父级?

时间:2015-04-29 13:16:15

标签: android image android-layout android-xml scaletype

我正在处理显示图像的应用程序,我需要创建一个以1:1宽高比显示大图像的视图。

首先,我有缩略图,72x72分辨率。我已将它们放在ImageView中,但即使我添加参数android:width="fill_parent"android:height="wrap_content",图像也不会是ImageView的大小,而只是居中于中期。

我尝试添加参数android:scaleType="centerCrop",它可以水平调整图像大小,但高度保持不变。

我如何设置ImageView以便它在所有设备上的宽度与父视图一样,并且它也应该是高度。

我可以以编程方式执行此操作,但我需要能够以XML格式执行此操作。

我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    // The ImageView I am talking about above
    <ImageView
        android:id="@+id/invitation_imageview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:scaleType="centerCrop"
        android:src="@drawable/face_woman_smile" />

    <ImageView
        android:id="@+id/invitation_avatar_view_1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:src="@drawable/face_woman_smile"
        android:scaleType="center"
        android:layout_margin="1dp" />

</LinearLayout>

4 个答案:

答案 0 :(得分:9)

我认为你需要将它添加到你的ImageView

android:adjustViewBounds="true"

答案 1 :(得分:2)

在保持宽高比的同时使图像拉伸到父宽度

设置

1- android:layout_widthmatch_parent

2- layout_heightwrap_content

3- adjustViewBoundstrue

像这样:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/unknown"
    android:adjustViewBounds="true" />

注意:这可能无法在预览中正确呈现,但可以在运行时使用。

答案 2 :(得分:1)

使用:

android:scaleType="fitXY"

答案 3 :(得分:1)

正如您所提到的,我通常使用android:width="match_parent"android:height="wrap_content",但我使用android:scaleType作为ImageView布局。

您可以详细了解here