在父ViewGroup的onSizeChanged中重新定位子视图

时间:2014-10-24 13:15:04

标签: android android-layout android-view absolutelayout android-viewgroup

我已将3个自定义视图(SmallTile.java)添加到另一个自定义视图(MyView.java;它基于已弃用的AbsoluteLayout - 但我计划稍后更改基类。

我试图通过父母中的以下代码来定位3个孩子:

@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    Log.d(APP, "w=" + w + "; h=" + h + ", oldw=" + oldw + ", oldh=" + oldh);

    int count = getChildCount();

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() == GONE)
            return;

        int x = mRandom.nextInt(w - child.getWidth());
        int y = mRandom.nextInt(h - child.getHeight());
        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, 
            x, 
            y
        );

        Log.d(APP, i + ": x=" + x + "; y=" + y);
    }

    requestLayout();
}

然而,尽管我可以在调试器中看到可行的位置 -

console

在模拟器中,所有孩子都定位错误 - 在原点:

emulator

这里出了什么问题?我想调用requestLayout()会调用AbsoluteLayout.onLayout(),从而重新绘制新随机位置的孩子?

以下是来自互联网的View生命周期的随机图片(我知道,它不是100%真实):

View lifecycle

0 个答案:

没有答案