android imageview wrap_content

7
大家好,感谢您的帮助。
我在Android Studio中有一个项目。
我在垂直LinearLayout中使用了一个ImageView。
以下是我的参数;
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/myimg"
/>

你可以在下面看到我的图片(红色块)和ImageView边框(蓝线)。宽度没有问题,它是布局的100%,但是我希望我的图像在保持长宽比的情况下适应区域。正如你在图像中所看到的,有白色空间,ImageView的高度比我的图像要大,如何将图像适配到ImageView中? 我想这样做是因为我希望即使手机是横向或纵向的,图像也能适应宽度。

不要使用android:src,而是使用android:background,然后进行检查。这样可能会得到你所需的效果。 - Ram
5个回答

27

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


你救了我,兄弟。 - Heshan Sandeepa

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"
     />

enter image description here


0
在ImageView中,有一个居中裁剪的选项 -
android:scaleType="centerCrop"

它会从中心裁剪图像。输出会像这样 -

resized


0

ImageView 的大小是正确的。您需要相应地缩放图像。ImageView 具有缩放属性。尝试使用 android:scaleType="fitXY",然后让我知道结果。


当我使用“fitXY”时,我的图像的纵横比正在改变,我的图像看起来很糟糕。 - yucel

0

对于ImageViewandroid:scaleType="fitXY"会失去它的纵横比。最好使用android:scaleType="fitCenter"并将imageView的背景设置为透明。

如果您不想保持纵横比,则可以使用setbackground()fitxy来满足您的要求。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接