如何去除imageView上下的额外空间?

86

我在一个RelativeLayout中有一个非常简单的图像,但是出现了额外的上下间距,我无法去掉。如何清除它?

这是它的样子:

Image from Eclipse preview

这里是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/slice11pp" />
</RelativeLayout>
11个回答

291
尝试这个:
``android:adjustViewBounds="true"``

在我的代码中,这只能使用 'android:layout_width="match_parent" android:layout_height="match_parent"'。 - Tapa Save
4
谢谢。太棒了,非常有用。 - Farruh Habibullaev

10
在你的上述imageView中,只需添加android:scaleType="fitXY"即可。

2
将其更改为android:scaleType="fitXY"。 - user1616685
5
这将改变纵横比,使物体看起来被拉伸或压缩。 - Cory Roy

5

必须足够添加属性:

android:adjustViewBounds="true"

例如:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/slice11pp"
        android:adjustViewBounds="true" />


5

你的问题可能是没有设置比例类型...在ImageView的XML中添加它,"fitCenter"应该是正确的,但还有其他选项,请查看:ScaleType

<ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/slice11pp"
            android:scaleType="fitCenter" />

1
在我的情况下,我还需要设置android:adjustViewBounds="true",正如这个问题中所解释的那样:http://stackoverflow.com/questions/9701685/empty-space-above-imageview?rq=1 - AlvaroSantisteban

3

如果没有特定的图片要求,可以使用drawable-nodpi文件夹。然后android:adjustViewBounds="true"将作为默认设置。

如果使用drawable-nodpi,则不需要设置android:adjustViewBounds="true"

我认为这是最轻松的方法。


3
如果您的ImageView视图高度设置为match_parent,则即使设置了android:adjustViewBounds="true",ImageView在顶部和底部会添加一些额外空白。因此,请将ImageView高度更改为wrap_content,并设置android:adjustViewBounds="true"。
例如:
<ImageView
  android:id="@+id/img_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"/>

3

这行代码将解决问题。

android:adjustViewBounds="true"

不要忘记旋转图片


1
这个问题在很多年前就已经被回答了! - Eduard Unruh

2

通过添加android:scaleType="centerCrop",可以实现居中裁剪的效果。

<ImageView
    android:id="@+id/image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/temp_image"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"/>

这对我有用之前和之后的图片 Before and After Image


0

这是完美的解决方案

 <ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>

它不会在任何一侧留下额外的单个数据点,但如果它不是纵向图像,则图像将会被扭曲。


0

只需在ImageView中添加ScaleType="fitxy"即可


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