24

I have a simple LinearLayout in Android with two images vertically:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.eataly.android" 
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_header1"
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_header2"
        />
</LinearLayout>

As you can see at the following link, I can't get rid of a a gap at the top and at the bottom of the images: http://img185.imageshack.us/img185/8484/senzanomev.png

I've tried everything imaginable on the LinearLayout and the ImageViews, i.e.:

android:padding="0px"
android:top="0px"
android:bottom="0px"
android:top="0px"
android:layout_margin="0px"

with no success. What can I do to remove these empty borders?

Enrico Detoma
  • 3,159
  • 3
  • 37
  • 53
  • 1
    have you tried negative values for layout_marginTop? – Asahi Aug 31 '10 at 20:51
  • oops, just edited my comment: it DOES work, but I'm not sure which value (in dp) to set. Should I just try until they align? – Enrico Detoma Sep 01 '10 at 07:54
  • 1
    I had a similar problem and I had to try until found a proper value. If anyone can suggest a better way - very welcome! – Asahi Sep 01 '10 at 08:51

3 Answers3

69

I found the solution. I needed to add this attribute to the ImageView:

android:adjustViewBounds="true"
Enrico Detoma
  • 3,159
  • 3
  • 37
  • 53
4

add android:scaleType="fitXY" in xml layout.

RaviPatidar
  • 1,438
  • 1
  • 18
  • 29
  • Thanks, I had `android:adjustViewBounds="true"` `android:scaleType="fitEnd"` and changed it to "fitXY". – CoolMind Jul 28 '16 at 17:54
0

To do this in a linearlayout
for the top ImageView: android:gravity="bottom"
for the bottom ImageView: android:gravity="top"
Are you sure you don't want to be using a RelativeLayout? If you're trying to align two images to each other, that seems the way to go for down the road imho.

QRohlf
  • 2,795
  • 3
  • 24
  • 27
  • 1
    RelativeLayout doesn't seem to solve the problem: it seems that the ImageView is higher than it should be, so both LinearLayout and RelativeLayout perform the same way. Actually the suggestion above (setting negative values for layout_marginTop) seems to work instead, so it must be an ImageView's problem, not a layout problem. – Enrico Detoma Sep 01 '10 at 08:06