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

17

我正在使用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个回答

53

我曾经也遇到过这个问题。在苦苦挣扎一段时间后,我按照这个的方法解决了它。

并且将下面的代码添加到我的程序中。

 android:adjustViewBounds="true"

它运行得非常完美。


15
这是因为图像被缩小以适应可用区域而不失去图像的长宽比。您会得到白色空间,因为图像首先占据了可用宽度,并根据原始图像的长宽比将高度下降。
为了更清楚地理解,请执行以下操作:
- 将相对布局的背景颜色设置为某种颜色
现在,您可以理解imageView与relativeLayout之间的空间。
一旦在设备上检查过这一点,请进行以下更改并重试:
- 在imageView中设置scaleType =“fitXY”属性。
这将使图像缩放并占用imageView可用的完整区域。(在此情况下,原始图像的长宽比将丢失)。现在,您将知道imageView占用的区域量。
我想一旦您这样做,您可以得出以下结论:
- 如果在第一次运行时将relativeLayout的背景设置为黑色,则由于imageView占据了整个区域而没有留下任何间隙,因此它将不可见。 - 在第二次运行中,图像将覆盖整个高度和宽度,尽管长宽比已丢失。因此,这证实了imageView确实覆盖了宽度和高度,没有留下任何空间,问题出在于图像的长宽比。
如果您得出完全不同的结果,请通知我们,我们可以解决它。
编辑:
- 请删除您为imageView设置的填充。

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