7

大家好,感谢您的帮助。

我在 Android Studio 中有一个项目。

我在垂直 LinearLayout 中使用 ImageView。

这是我的参数;

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/myimg"
/>

您可以在下方看到我的图像(红色块)和图像视图边框(蓝线),宽度没有问题,它是我布局的 %100,但我希望我的图像适合该区域,同时保持纵横比。正如您在图像中看到的那样,有空格,ImageView 的高度比我的图像大,我怎样才能在 ImageView 中放置图像?

我想要这个,因为我希望该图像适合宽度,即使手机是纵向或横向的。

在此处输入图像描述

4

5 回答 5

25

我找到了解决方案 android:adjustViewBounds="true"

于 2014-09-23T19:06:50.130 回答
0

的大小imageview是正确的。您必须相应地缩放图像。ImageView具有缩放属性。试试这个android:scaleType="fitXY",让我知道。

于 2014-09-23T13:00:41.093 回答
0

你的代码是写的,但我不知道父布局,你的图像背景是白色的,红色部分在这个背景的中间。

这是与父布局相关的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">



<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/red"
     />

在此处输入图像描述

于 2014-09-23T13:07:52.310 回答
0

对于ImageView,android:scaleType="fitXY"将失去纵横比。最好使用android:scaleType="fitCenter"和设置背景imageView transparent

如果您不想保持纵横比,那么setbackground()fitxy将满足您的要求

于 2014-09-23T13:09:02.257 回答
0

在 imageview 中,有一个中心裁剪选项 -

android:scaleType="centerCrop"

它会做的是从中心裁剪图像。输出将像 -

调整大小

于 2014-09-23T13:13:06.007 回答