我有一个带有自定义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>
我调试了子视图的measuredHeight
和。height
我发现它们不相等。它很奇怪。
我想知道我的代码有什么问题。我该如何解决?