17

我正在使用相对布局来覆盖图像。到目前为止,在我测试过的所有屏幕尺寸上(从 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>
4

2 回答 2

52

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

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

 android:adjustViewBounds="true"

它完美地工作

于 2014-11-10T09:20:57.600 回答
15

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

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

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

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

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

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

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

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

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

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

如果您得到完全不同的结果,请告知,我们可以解决

编辑 :

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

于 2013-07-31T10:24:42.857 回答