Android 为什么我在RelativeLayout中的ImageView的顶部和底部都有空白?

Android 为什么我在RelativeLayout中的ImageView的顶部和底部都有空白?,android,android-linearlayout,android-relativelayout,Android,Android Linearlayout,Android Relativelayout,我正在使用relativelayout来覆盖图像。到目前为止,在我测试过的所有屏幕尺寸(从2.7到10.1英寸)上,我的图像顶部总是有空白。在我的IDE中,我总是看到我的relativelayout在图像的顶部和底部造成了额外的空间 为什么呢?我已经将所有高度属性设置为wrap\u content,甚至添加了adjustViewBounds属性 注意:您应该知道我的图像的大小要大得多,这意味着会有一些缩放 谢谢你的提示 代码如下: <?xml version="1.0" encoding=

我正在使用relativelayout来覆盖图像。到目前为止,在我测试过的所有屏幕尺寸(从2.7到10.1英寸)上,我的图像顶部总是有空白。在我的IDE中,我总是看到我的relativelayout在图像的顶部和底部造成了额外的空间

为什么呢?我已经将所有高度属性设置为
wrap\u content
,甚至添加了
adjustViewBounds
属性

注意:您应该知道我的图像的大小要大得多,这意味着会有一些缩放

谢谢你的提示

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" 
    android:focusable="true"
android:focusableInTouchMode="true">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/bgf"
    android:textColor="#000000"
    android:textSize="25sp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:adjustViewBounds="true"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/cde"
        android:contentDescription="@string/cde" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
        android:src="@drawable/fgh"
        android:contentDescription="@string/abc" />

</RelativeLayout>


</LinearLayout>
</ScrollView>

当图像缩小以适应可用区域而不丢失图像的纵横比时,会发生这种情况。您将获得空白,因为图像首先占用了可用宽度,并且根据原始图像的纵横比降低了图像的高度

为了更清楚地理解,我建议您执行以下操作:

  • 将相对布局的背景色设置为某种颜色
现在您可以了解imageView和relativelayout之间的空间

在设备上检查后,执行以下更改并重试

  • 在imageView中设置属性scaleType=“fitXY”
这将使图像缩放并占据imageView可用的完整区域。(在这种情况下,原始图像的纵横比将丢失)。现在您将了解imageView占用的面积

我想,一旦你这样做了,你就可以得出结论:

  • 在第一次运行时,如果您将relativeLayout的背景设置为黑色,它将不可见,因为imageView占据了整个区域而没有留下任何间隙

  • 在第二次运行中,图像将覆盖整个高度和宽度,尽管纵横比丢失。因此,这确定了imageView确实覆盖了宽度和高度,并且没有留下任何空间,这就是图像的纵横比问题所在

  • 如果你得出完全不同的结果,请务必通知我们,我们可以解决

    编辑:


    请务必移除您为imageView提供的填充物

    我遇到了确切的问题。经过一段时间的努力,我通过以下方法解决了这个问题

    并将以下内容添加到我的程序中

     android:adjustViewBounds="true"
    
    它工作得很好