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

Android 在父视图组的onSizeChanged中重新定位子视图,android,android-layout,android-view,absolutelayout,android-viewgroup,Android,Android Layout,Android View,Absolutelayout,Android Viewgroup,我已将3个自定义视图()添加到另一个自定义视图(;它基于不推荐的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="

我已将3个自定义视图()添加到另一个自定义视图(;它基于不推荐的
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();
}
@覆盖
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
Log.d(应用程序,“w=“+w+”;h=“+h+”,oldw=“+oldw+”,oldh=“+oldh”);
int count=getChildCount();
for(int i=0;i
然而,即使我能在调试器中看到可行的位置-

在emulator中,所有子对象的位置都错误-在原点:

这里出了什么问题?我以为调用
requestLayout()
会调用并因此在新的随机位置重新绘制子对象

下面是一张来自internet的视图生命周期的随机图片(我意识到,这不是100%正确):


hmm,您在哪里为儿童设置位置?您只需创建
参数
,而不使用它。。。另外,您不使用子视图的setX/Y。。。(此解决方案需要API>=11)。。。但如果你想问我,在船上的每封信都使用View是否好,我会回答:不。。。选择自定义抽屉视图(甚至是一些带有精灵的2d图形库)+1谢谢,Selvin-我已经删除了
requestLayout()
,并添加了
child.setLayoutParams(params)