2010-02-24 26 views
3

我遇到了問題。動態添加TableRow與getMeasuredHeight/getHeight的問題

我希望動態地將行添加到TableLayout。添加這些行時,我需要能夠獲得視圖的大小,因此我試圖在onSizeChanged()期間執行此操作,但新行不顯示。所以我試過onFinishInflate(),但是我沒有權限訪問(getMeasuredHeight returns 0)的大小。

以下代碼的輸出顯示兩行initial row + onFinishInflate getMeasuredHeight=0。但如果我使用從SDK的hierarchyviewer,並按load view hierarchy然後我突然看到3行在模擬器initial row + onFinishInflate getMeasuredHeight=0 + onSizeChanged getMeasuredHeight=270!。

注:這是使用模擬器2.1 hvga景觀,代碼內置 最新sdk,瞄準android1.5。


<?xml version="1.0" encoding="utf-8"?> 
<com.steelbytes.android.TableHeightTest.TestLinLay 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TableLayout 
     android:id="@+id/table1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
     <TableRow 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       > 
       <TextView 
        android:text="initial row" 
       /> 
      </TableRow> 
     </TableLayout> 
</com.steelbytes.android.TableHeightTest.TestLinLay> 

package com.steelbytes.android.TableHeightTest; 

import android.app.Activity; 
import android.os.Bundle; 

public class TableHeightTest extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

package com.steelbytes.android.TableHeightTest; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class TestLinLay extends LinearLayout 
{ 
Context mContext; 

public TestLinLay(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    mContext = context; 
} 

    private void addRow(String funcName) 
    { 
     TableLayout tl = (TableLayout)findViewById(R.id.table1); 
     TableRow tr = new TableRow(mContext); 
     TextView tv = new TextView(mContext); 
     tv.setText(funcName+" getMeasuredHeight="+tl.getMeasuredHeight()); 
     tr.addView(tv); 
     tl.addView(tr); 
    } 

    @Override 
    protected void onFinishInflate() 
    { 
     super.onFinishInflate(); 
     addRow("onFinishInflate"); 
    } 

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

+0

忘了提,增加requestLayout()的調用和/或無效()沒有幫助。 – SteelBytes 2010-02-24 06:01:12

回答

1

我有同樣的問題。最終我偶然發現了以下「規則」: 在「佈局傳遞」(onSizeChanged())期間,您不能/不應該requestLayout(),正確的做法是「發佈Runnable」後來。這裏是我的代碼,我用我的時候onSizeChanged()重寫結束(我調整我的所有按鈕)

// Note: 'posting a Runnable' is required to call requestLayout(), 
// not supposed to call requestLayout() during a 
// layout pass (onSizeChanged()) 
private Handler handler;  // for posting request(s) 

(In constructor code) 
{ 
    handler = new Handler(); 
} 

/* 
* Post request to run a loop requesting button layout so 
* the buttons will be drawn using their new size. 
*/ 
private void postLayoutRequest() { 
handler.post(new Runnable() { 
    @Override public void run() { 
    for(int i = BTN_BASE_ID; i <= TOP_BUTTON_ID; ++i) { 
      ButtonEx btn = (ButtonEx)findViewById(i); 
      btn.requestLayout(); 
     } 
    } 
}); 
} 
+0

而不是處理程序可以使用[View.post](http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable))。非常感謝解釋。你節省了我的一天。 – neworld 2012-10-11 11:28:39