3

onMeasureカスタムメソッドを使用したカスタム Relativelayout があります。以下のようなコード。

@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);
    }

上記のコードの目的は、設定することheight=width*ratioです。

それをした後。layout_height値を持つ子ビューはmatch_parent、親の高さを埋めません。のように動作し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>

そして、子ビューのmeasuredHeightandをデバッグします。heightそれらは等しくないことがわかりました。変です。

コードの何が問題なのか知りたいです。どうすれば修正できますか?

4

0 に答える 0