Why do I get white space on top and on the bottom of my ImageView in my RelativeLayout?

user2426316 picture user2426316 · Jul 31, 2013 · Viewed 19.7k times · Source

I am using a relativelayout to overlay to images. On all screen sizes so far that I have tested (from 2.7 to 10.1 inch) I always get white space on top of my image. In my IDE I always see that my relativelayout is causing the extra space on top and on the bottom of my image.

Why is that? I have set all height attributes to wrap_content and even added the adjustViewBounds attribute.

Note: You should know that my image is a lot bigger in size, meaning that there will be some sort of scaling.

Thanks for your tips!

Here is the code:

<?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>

Answer

Sunny Poong picture Sunny Poong · Nov 10, 2014

I had the exact problem. After struggling for quite some time, I solved it by following this

and add the following to my program

 android:adjustViewBounds="true"

It works perfectly