3

I have a custom Relativelayout with custom onMeasure method. code as below.

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = (int) (getMeasuredWidth() * ratio);
        measureChildren(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        setMeasuredDimension(getMeasuredWidth(), height);
    }

The purpose of above code is to set height=width*ratio.

After doing that. Child view with layout_height value match_parent will not be fill the parent's height. It works like wrap_content.

    <com.baidu.mall.ui.view.RatioRelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/orange"
        app:riv_ratio=".615384615">

        <ImageView
            android:id="@+id/selected_item_grid_item0_image_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/red"
            android:scaleType="centerCrop" />
    </com.baidu.mall.ui.view.RatioRelativeLayout>

And I debug the measuredHeight and height of child view. I found they are not equal. It is strange.

I want to know what's wrong with my code. And how I can fix it?

Cao Dongping
  • 969
  • 1
  • 12
  • 29

0 Answers0