ImageView should be full screen but has wrong height

1.8k views Asked by At

I am working on a simple app that shows 4x3-aspect images in the lower half of the screen in portrait orientation, and filling the screen in landscape orientation. Portrait works and the 320x240 images (which are loaded over the net) are shown that size.

Rotate to landscape and I use a separate layout in res/layout-land which omits the upper half of the portrait layout and places the ImageSwitcher full screen. Some translucent controls overlay the image, so this uses a merge tag in the layout xml with the ImageSwitcher and a RadioGroup:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="fill">
   </ImageSwitcher>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:orientation="horizontal">
    ...
</merge>

The ImageViews in the ImageSwitcher are loaded from a separate layout:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="fill"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:contentDescription="@null">
</ImageView>

For some reason the image views don't fill the screen however. Inspecting the view hierarchy with hierarchyviewer, I see that the ImageSwitcher has dimensions 480x295 (full screen minus the top bar), but the contained ImageViews have dimensions 480x240.

I've struggled with this for a couple of hours, fiddling with all the layout settings in the ImageSwitcher and the ImageViews but I can't get the ImageView to match the 480x295 dimension of its parent. Any help or insight appreciated.

1

There are 1 answers

0
Neil Gall On BEST ANSWER

I found the problem. I was inflating the ImageViews in the ImageSwitcher from XML layouts, but this doesn't seem to work. Creating the ImageViews in code and assigning the appropriate FrameLayout.LayoutParams for each does work.

hierarchyviewer was a big help here - it highlighted that the layout params on the ImageViews were not what I was expecting.