使ImageView(及其src)适合布局宽度并使其高度成比例

Add*_*dev 11 java layout android

你能帮我配置我的布局吗?我没有达到我想要的目标:

我将imageview背景设置为红色以解释我想要的内容:

图像的宽度小于布局宽度.我希望图像按比例增长保持其纵横比直到达到第一张图像的方面(我用photoshop手动修改)但是当设置width ="fill_parent"时,结果是第二张图像并将宽度和高度设置为" fill_parent"结果是第三张图片.

我试图与ScaleType结合使用但没有成功

我怎样才能实现第一个选择?谢谢

在此输入图像描述

Ron*_*nie 14

android:adjustViewBounds=true在xml中使用attr forImageView


这个问题通过扩展ImageView类来解决.

Android ImageView调整父级的高度和拟合宽度


我创建了一个样本.这个对我有用.这是xml布局文件.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:src="@drawable/hydrangeas"
        android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:background="@android:color/white"
        android:layout_width="wrap_content"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

它也适用fitCenter.看看快照.
使用centerInside和adjustViewBounds =


Mar*_*los 2

在 ImageView 标记中使用属性 fit_center。

<ImageView ... android:scaleType="fitCenter" .. />
Run Code Online (Sandbox Code Playgroud)