是Android编程的新手,刚刚开始学习它过去6周,我正在为Android编写一个扫雷游戏,我已经设法做了一些游戏的部分而没有太多问题。但是,我必须使用TableLayout和TableRow以编程方式设计网格并在其中插入按钮;所以我写了几行代码来做到这一点,但每当我运行游戏时,我都会收到“确认透视切换”错误。
以下是我写的代码 -
` public class Game extends Activity implements OnClickListener {
Button[][] btn = new Button[6][6];
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gamegrid);
int i, j;
LinearLayout layoutVertical = (LinearLayout) findViewById(R.layout.gamegrid);
//create a new TableLayout
TableLayout table = null;
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
LayoutParams param = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for(i = 0; i <6; i++){
table = new TableLayout(this);
table.setWeightSum(5);
layoutVertical.addView(table, param);
for(j=0; j<7; j++){
btn[i][j] = new Button(this);
table.addView(btn[i][j], param);
btn[i][j].setOnClickListener(this);
}
} return;
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
} `
我认为我的问题是以下几行 -
`for(i = 0; i <6; i++){
table = new TableLayout(this);
table.setWeightSum(5);
layoutVertical.addView(table, param);
for(j=0; j<7; j++){
btn[i][j] = new Button(this);
table.addView(btn[i][j], param);
btn[i][j].setOnClickListener(this);
}
}`
假设创建按钮然后将它们存储在按钮数组中,然后在TableLayout中插入按钮!
为什么我会收到上述错误?
你可以帮我指出你做错了什么吗?因为我没有任何错误显示。由于
答案 0 :(得分:2)
如果您想使用TableLayout,那么您需要构建的内容如下所示(使用4x4网格作为示例)
TableLayout
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
1 TableLayout包含4个TableRows,每行包含4个按钮(4x4网格)。 在代码中它可能是这样的:
Button[][] buttonArray = new Button[4][4];
TableLayout table = new TableLayout(context);
for (int row = 0; row < 4; row++) {
TableRow currentRow = new TableRow(context);
for (int button = 0; button < 4; button++) {
Button currentButton = new Button(context);
// you could initialize them here
currentButton.setOnClickListener(listener);
// you can store them
buttonArray[row][button] = currentButton;
// and you have to add them to the TableRow
currentRow.addView(currentButton);
}
// a new row has been constructed -> add to table
table.addView(currentRow);
}
// and finally takes that new table and add it to your layout.
layoutVertical.addView(table);
答案 1 :(得分:0)
您正在添加多个表,而不是具有多个行/列的表。流程应该是这样的:
final LinearLayout layout = (LinearLayout)activity.findViewById(com.myapp.tableContainer);
final TableLayout table = new TableLayout(context);
layout.add(table, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
final TableRow row = new TableRow(context);
TextView textView = new TextView(context);
textView.setText("0");
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
row.addView(textView, 0);
textView = new TextView(context);
textView.setText("1");
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
row.addView(textView, 1);
table.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1));
继续以相同的方式添加更多行。