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

use*_*316 16 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>
Run Code Online (Sandbox Code Playgroud)

小智 48

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

**/sf/answers/1550107401/

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

 android:adjustViewBounds="true"
Run Code Online (Sandbox Code Playgroud)

它完美地运作


pvn*_*pvn 13

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

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

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

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

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

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

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

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

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

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

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

编辑:

请删除你为imageView提供的填充