我有TableLyout
(在XML文件中),我正在尝试添加表行。每行包含按钮,其编号由参数控制。
每次运行此代码时,程序都会退出。这应该是一项简单的任务。出了什么问题?
爪哇:
public class Play extends Activity{
int status=0 , timeDelay , row , col;
TextView score;
TableRow rows [];
TableLayout table;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_play);
setLevel();
table = (TableLayout)findViewById(R.id.Table_cards);
for(int i=0 ; i<row ; i++){
TableRow tblr = new TableRow(this);
for(int j=0 ; j<col ; j++){
final ImageButton b = new ImageButton(this);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.setBackgroundResource(R.drawable.card_back_blue);
}
});
b.setBackgroundResource(R.drawable.c1+j);
tblr.addView(b);
}
table.addView(tblr);
}
}
XML文件:
<TableLayout
android:id="@+id/Table_cards"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/layout_up"
android:background="@drawable/wss"
android:gravity="center_vertical"
android:orientation="vertical" >
</TableLayout>
答案 0 :(得分:0)
此代码效果很好,
public class Play extends Activity{
private int status=0 , timeDelay , row=10, col=10;
private TextView score;
private TableRow rows [];
private TableLayout table;
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.table_dynamic);
// setLevel();
table = (TableLayout)findViewById(R.id.tableLayout);
for(int i=0 ; i<row ; i++){
TableRow tblr = new TableRow(this);
for(int j=0 ; j<col ; j++){
final ImageButton b = new ImageButton(this);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// b.setBackgroundResource(R.drawable.card_back_blue);
}
});
// b.setBackgroundResource(R.drawable.c1+j);
tblr.addView(b);
}
table.addView(tblr);
}
}
}
布局table_dynamic,
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>