0

I have a custom view extended from RelativeLayout where the size should be square, and a background image with match_parent for it's width and height as it's child. There is also a button that should be centered horizontally and aligned with the background image's bottom.

Imgur

All works well, until I add in some margin and rotate my device into landscape mode. In landscape mode, the container's width somehow gets larger than its height, as shown below:

Imgur

The custom view is just a simple class extended from RelativeLayout, and I've overridden its onMeasure method as follows:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int size = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    int specSize = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);

    super.onMeasure(specSize, specSize);

    measureChild(bgImage, specSize, specSize);  // bgImage is the flower image

    setMeasuredDimension(size, size);

}

And the layout for the view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:background="#eff1f1" android:padding="1dp">


<com.example.MyViewGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#e54eff"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" android:layout_margin="50dp">

    <ImageView
            android:id="@+id/myPic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/flower"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:adjustViewBounds="true"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:id="@+id/button" android:layout_alignBottom="@+id/myPic" android:layout_centerHorizontal="true"/>
</com.example.MyViewGroup>



</RelativeLayout>

Can anyone point out what I am doing wrong here? If this question has been answered before, could someone kindly give me a link? I couldn't seem to find any similar problems after searching. I'm developing for API 11 and testing on a Samsung Galaxy Tab 8.9 running Android 4.04.

Thanks!

acidleaf
  • 21
  • 4
  • Remove the `measureChild(bgImage, specSize, specSize);` and `setMeasuredDimension(size, size);` lines. – user May 19 '14 at 08:10
  • I removed those lines and the problem still persists. – acidleaf May 19 '14 at 08:17
  • How big is the image? Remove the `adjustViewBounds` attribute. – user May 19 '14 at 08:35
  • I have removed the `adjustViewBounds` attribute and nothing changed. The image's original size is 512x512. In the view, echoing `MeasureSpec.getSize()` gives me 600+ for both width and height. The extra space in landscape mode is roughly the same as twice the amount of margins I have. Should I consider the margin in `onMeasure`? – acidleaf May 19 '14 at 08:45
  • The margin is outside the view so it shouldn't influence the dimensions. Did you measured the dimensions of the `ImageView`? I mean looking at the `getMeasuredWidth/Height()` of the `ImageView` after you call the `super.onMeasure()` in the custom `RelativeLayout` comparing with the values of the actual `getMeasuredWidth/Height()` of the custom component. – user May 19 '14 at 09:07
  • Yup, echoing `getMeasuredWidth/Height()` after `super.onMeasure` gives the correct(intended) size. The output in `onLayout` is the one which gives me a larger width than height in landscape mode. – acidleaf May 19 '14 at 09:16

0 Answers0