Android 2.x setLayoutParams在旋转时不应用?

Android 2.x setLayoutParams在旋转时不应用?,android,user-interface,Android,User Interface,我有一个处理方向更改的活动,我想在设备旋转时手动调整布局大小。在该布局的onLayout中,我调用setLayoutParams: @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed) { int or

我有一个处理方向更改的活动,我想在设备旋转时手动调整布局大小。在该布局的onLayout中,我调用setLayoutParams:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    if (changed) {
        int orientation = getResources().getConfiguration().orientation;
        // Only resize on actual orientation change
        if (orientation != mLastOrientation) {
            // Apply the new layout size
            setLayoutParams(new LinearLayout.LayoutParams(someNewWidth, someNewHeight);
            mLastOrientation = orientation;
        }
    }
}
这在我较新的4.x设备上运行良好,但在2.x设备上,setLayoutParams似乎运行一个方向“延迟”

因此,在从纵向到横向的第一次旋转中,调整大小不会发生,然后在随后的旋转中,它会在纵向上显示横向大小,在横向上显示纵向大小,等等

我在setLayoutParams源代码中读到它调用requestLayout,它应该重新绘制布局,但似乎没有立即这样做。我还尝试了invalidate(),它也不起作用


关于为什么setLayoutParams没有应用于第一次旋转或其他解决方案,您有什么想法吗?

在布局过程中更改
LayoutParams
会导致多个布局过程完成,并且在显示复杂布局时会导致严重减速


覆盖视图的
onMeasure()
方法并在那里设置测量的大小更有效。请看此答案以获取此技术的示例。

My
onMeasure()
函数在旋转时似乎没有更改widthMeasureSpec和heightMeasureSpec值。我假设我需要自己传递测量值,如下所示:“如果您需要覆盖自定义视图组的onMeasure,请更改widthMode、widthSize、heightMode和heightSize,使用MeasureSpec.MakeMasureSpec将它们编译回MeasureSpec,并将生成的整数传递给super.onMeasure。”太棒了,成功了!我还必须将创建的MeasureSpec传递给视图的measureChildren。谢谢!