2013-10-07 23 views
7

從官方文檔:爲什麼在更改= false時重複調用view.onLayout()?

Android Docs - View

protected void onLayout (boolean changed, int left, int top, int right, int bottom) 

Parameters 
changed This is a new size or position for this view 
left Left position, relative to parent 
top  Top position, relative to parent 
right Right position, relative to parent 
bottom Bottom position, relative to parent 

爲什麼我的自定義視圖與changed=false反覆叫什麼?這是正常的嗎?

這裏的調用樹:

Synoptic.onLayout(boolean, int, int, int, int) line: 130  
Synoptic(View).layout(int, int, int, int) line: 11282 
Synoptic(ViewGroup).layout(int, int, int, int) line: 4224 
RelativeLayout.onLayout(boolean, int, int, int, int) line: 925 
RelativeLayout(View).layout(int, int, int, int) line: 11282 
RelativeLayout(ViewGroup).layout(int, int, int, int) line: 4224 
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutHorizontal() line: 1617 
LinearLayout.onLayout(boolean, int, int, int, int) line: 1401 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
SwipeView(FrameLayout).onLayout(boolean, int, int, int, int) line: 431 
SwipeView(HorizontalScrollView).onLayout(boolean, int, int, int, int) line: 1382  
SwipeView.onLayout(boolean, int, int, int, int) line: 154 
SwipeView(View).layout(int, int, int, int) line: 11282 
SwipeView(ViewGroup).layout(int, int, int, int) line: 4224 
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutVertical() line: 1486  
LinearLayout.onLayout(boolean, int, int, int, int) line: 1399 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
FrameLayout.onLayout(boolean, int, int, int, int) line: 431 
FrameLayout(View).layout(int, int, int, int) line: 11282  
FrameLayout(ViewGroup).layout(int, int, int, int) line: 4224  
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutVertical() line: 1486  
LinearLayout.onLayout(boolean, int, int, int, int) line: 1399 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 431 
PhoneWindow$DecorView(View).layout(int, int, int, int) line: 11282 
PhoneWindow$DecorView(ViewGroup).layout(int, int, int, int) line: 4224 
ViewRootImpl.performTraversals() line: 1514 
ViewRootImpl.handleMessage(Message) line: 2467 
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] 
+0

有人請求佈局? –

+0

我內部的其餘部分似乎是靜態的(沒有動畫),但是有很多相對佈局。 –

回答

6

我發現這個問題:對於像我這樣的複合成分,含有TextView,以.setText()重複調用導致給母公司onLayout()

一個電話......即使新的文本等於當前TextView文字!

所以我解決了:

public void setTextViewText(String text) { 
    if (!this.textViewValue.getText().equals(text)) { 
     this.textViewValue.setText(text); 
    } 
} 
10

這裏的關鍵部分來自文檔是:

從佈局時調用這一觀點應分配的大小和位置,它的每一個孩子的。

如果一個ViewGroup中的View被設置爲wrap_content並且它改變了它的大小(例如TextView中的文本改變爲更長的字符串),那麼ViewGroup中的onLayout就會出現。這是因爲視圖在佈局時會給出它們的大小,所以如果沒有這個調用,視圖將永遠不會被賦予新的大小。

相關問題