为什么我的RelativeLayout中的ImageView顶部和底部会出现空白区域?

时间:2013-07-31 10:12:52

标签: android android-linearlayout android-relativelayout

我使用relativelayout覆盖图像。到目前为止我所测试的所有屏幕尺寸(从2.7到10.1英寸)我总是在我的图像上方获得空白区域。在我的IDE中,我总是看到我的relativelayout导致了图像顶部和底部的额外空间。

为什么?我已将所有高度属性设置为wrap_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>

2 个答案:

答案 0 :(得分:44)

我遇到了确切的问题。经过一段时间的挣扎,我按照这个解决了它。

** https://stackoverflow.com/a/22144391/2405473

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

 android:adjustViewBounds="true"

完美无缺

答案 1 :(得分:12)

这是在图像按比例缩小以适应可用区域而不会丢失图像的宽高比时发生的。您正在获得空白区域,因为图像首先占用了可用的宽度,并根据原始图像的纵横比降低了它的高度。

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

  • 将相对布局的背景颜色设置为某种颜色

现在您可以了解imageView和relativelayout之间的空间。

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

  • 在imageView中设置属性scaleType =“fitXY”。

这将使图像缩放并占据imageView可用的完整区域。 (在这种情况下,原始图像的宽高比将丢失)。现在,您将了解imageView占用的区域数量。

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

  1. 在第一次运行中,如果您将relativeLayout的背景设置为黑色,则它将不可见,因为imageView占据整个区域而不留任何间隙。

  2. 在第二次运行中,图像将覆盖整个高度和宽度,但宽高比会丢失。因此,这确定了imageView确实覆盖了宽度和高度并且没有剩余空间,它的图像宽高比即问题

  3. 如果您完全得出不同的结果,请告知,我们可以解决这个问题

    编辑:

    请删除您为imageView提供的填充