自定义布局,onMeasure / onLayout连续调用

时间:2013-06-13 14:54:34

标签: java android layout custom-controls relativelayout

我编写了自己的布局(sublclassing RelativeLayout)。我从调试器中看到onMeasure()和onLayout()被连续调用。这是正常的行为吗?堆栈跟踪从dispatchMessage()开始:

Synoptic.onMeasure(int, int) line: 125  
Synoptic(View).measure(int, int) line: 12727    
RelativeLayout.measureChildHorizontal(View, RelativeLayout$LayoutParams, int, int) line: 594    
RelativeLayout.onMeasure(int, int) line: 376    
RelativeLayout(View).measure(int, int) line: 12727  
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4698    
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1369 
LinearLayout.measureHorizontal(int, int) line: 1017 
LinearLayout.onMeasure(int, int) line: 555  
LinearLayout(View).measure(int, int) line: 12727    
SwipeView(HorizontalScrollView).measureChildWithMargins(View, int, int, int, int) line: 1159    
SwipeView(FrameLayout).onMeasure(int, int) line: 293    
SwipeView(HorizontalScrollView).onMeasure(int, int) line: 303   
SwipeView(View).measure(int, int) line: 12727   
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4698    
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1369 
LinearLayout.measureVertical(int, int) line: 660    
LinearLayout.onMeasure(int, int) line: 553  
LinearLayout(View).measure(int, int) line: 12727    
LinearLayout.measureVertical(int, int) line: 812    
LinearLayout.onMeasure(int, int) line: 553  
LinearLayout(View).measure(int, int) line: 12727    
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4698 
FrameLayout.onMeasure(int, int) line: 293   
FrameLayout(View).measure(int, int) line: 12727 
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4698    
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1369 
LinearLayout.measureVertical(int, int) line: 660    
LinearLayout.onMeasure(int, int) line: 553  
LinearLayout(View).measure(int, int) line: 12727    
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4698   
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 293    
PhoneWindow$DecorView.onMeasure(int, int) line: 2092    
PhoneWindow$DecorView(View).measure(int, int) line: 12727   
ViewRootImpl.performTraversals() line: 1068 
ViewRootImpl.handleMessage(Message) line: 2446  
ViewRootImpl(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4424    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 511  
ZygoteInit$MethodAndArgsCaller.run() line: 784  
ZygoteInit.main(String[]) line: 551 
NativeStart.main(String[]) line: not available [native method]  

,此处为onLayout():

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

       if (changed) {

        itemGrid.getLayoutParams().width = (int) itemSize;
        itemGrid.getLayoutParams().height = (int) itemSize;

        itemProduction.getLayoutParams().width = (int) itemSize;
        itemProduction.getLayoutParams().height = (int) itemSize;

        itemConsumption.getLayoutParams().width = (int) itemSize;
        itemConsumption.getLayoutParams().height = (int) itemSize;

        imageViewFlowAnimation.getLayoutParams().height = (int) animationHeight;
        imageViewFlowAnimation.getLayoutParams().width = (int) animationWidth;
}   
    }

2 个答案:

答案 0 :(得分:1)

您不应该在任何一种方法中调用super。这就是无休止的循环。您还需要确保从setMeasuredDimension();致电onMeasure(),否则会引发异常。

答案 1 :(得分:1)

你试过这个: if(已更改)super.onLayout(已更改,l,t,r,b);