2012-06-02 47 views
0

我嘗試將按鈕動態添加到TableRow,但遇到錯誤。可以在android中動態地將控件添加到TableRow中嗎?

佈局XML

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/layout" > 
<TableRow android:id="@+id/jumble"> 
</TableRow> 
</TableLayout> 

錯誤線

TableRow tr = (TableRow) findViewById(R.id.jumble); 

有一個空的異常。

它甚至可以做到這一點?或者我將不得不動態添加TableRow到TableLayout?

回答

0

我相信你必須先找到父ViewGroup,然後從它得到子視圖。

TableLayout layout = (TableLayout) findViewById(R.id.layout); 
TableRow row = layout.findViewById(R.id.jumble); 
// add buttons to the row 

這假設您已將TableLayout設置爲其onCreate方法中活動的主佈局。

setContentView(R.layout.layout); 
相關問題