Android - do ViewGroup onDraw need to iterate through child view and call onDraw explicitly?

送分小仙女□ 提交于 2019-12-06 05:51:21

Did you overwrite onLayout? When Android lays out your ViewGroup, your ViewGroup is responsible for laying out the children.

This code is from a custom ViewGroup that lays out all children on top of each other:

@Override
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {

    int count = this.getChildCount();
    for (int i = 0; i < count; i++) {

        View child = this.getChildAt(i);
        child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
    }
}

For completeness, the onMeasure override:

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

    int parentWidth  = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(parentWidth, parentHeight);

    int count = this.getChildCount();
    for (int i = 0; i < count; i++) {

        View child = this.getChildAt(i);
        this.measureChild(
            child,
            MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(parentHeight, MeasureSpec.EXACTLY));
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!