Android 在测量时重写视图组,子项匹配\u父项不工作
我使用customAndroid 在测量时重写视图组,子项匹配\u父项不工作,android,viewgroup,onmeasure,Android,Viewgroup,Onmeasure,我使用customonMeasure方法创建了一个自定义Relativelayout。代码如下 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = (int) (getMeasuredWidth() * ratio
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);
}
上述代码的目的是设置高度=宽度*比率
这样做之后。具有layout\u height
值match\u parent
的子视图将不会填充父视图的高度。它的工作原理类似于wrap\u 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>
我调试了子视图的测量高度
和高度
。我发现他们不平等。真奇怪
我想知道我的代码出了什么问题。我怎样才能修好它