2012-06-02 48 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); 
相关问题