Java 在android中,如何通过编程将按钮数组添加到TableLayout中?
我是android编程新手,在过去的6周里刚刚开始学习,我正在为android编写一款扫雷游戏,我已经成功地完成了游戏的某些部分,没有太多问题。然而,我必须使用TableLayout和TableRow以编程方式设计网格,并在其中插入按钮;所以我写了几行代码来实现这一点,但每当我运行游戏时,我都会收到“确认透视切换”错误 这是我写的代码-Java 在android中,如何通过编程将按钮数组添加到TableLayout中?,java,android,Java,Android,我是android编程新手,在过去的6周里刚刚开始学习,我正在为android编写一款扫雷游戏,我已经成功地完成了游戏的某些部分,没有太多问题。然而,我必须使用TableLayout和TableRow以编程方式设计网格,并在其中插入按钮;所以我写了几行代码来实现这一点,但每当我运行游戏时,我都会收到“确认透视切换”错误 这是我写的代码- ` public class Game extends Activity implements OnClickListener { Butto
` 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
}
} `
`公共类游戏扩展活动实现OnClickListener{
按钮[][]btn=新按钮[6][6];
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gamegrid);
int i,j;
LinearLayout layoutVertical=(LinearLayout)findViewById(R.layout.gamegrid);
//创建新的表格布局
TableLayout table=null;
表.SetStretCharl列(真);
表.setShrinkAllColumns(真);
LayoutParams param=新建TableLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容);
对于(i=0;i您正在添加多个表,而不是一个包含许多行/列的表。流程应该如下所示:
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));
然后以相同的方式继续添加更多行。如果要使用TableLayout,则需要构造如下所示(以4x4网格为例)
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);
Button[][]buttonArray=新按钮[4][4];
TableLayout table=新的TableLayout(上下文);
用于(int行=0;行<4;行++){
TableRow currentRow=新的TableRow(上下文);
用于(int按钮=0;按钮<4;按钮++){
按钮currentButton=新按钮(上下文);
//你可以在这里初始化它们
currentButton.setOnClickListener(监听器);
//你可以储存它们
buttonArray[行][按钮]=当前按钮;
//您必须将它们添加到TableRow中
currentRow.addView(currentButton);
}
//已构造新行->添加到表
table.addView(当前行);
}
//最后,获取新表并将其添加到布局中。
layoutVertical.addView(表格);
非常感谢,我尝试了很多,效果很好!但是程序出现了异常问题,运行正常,但是当它执行网格活动时,什么都没有发生,我的屏幕一片空白!知道为什么吗?[很抱歉没有发布完整程序的代码]你在logcat中是否有任何错误或相关内容?不幸的是,没有,logcat中没有显示任何内容。不知道是什么问题。也许你根本没有设置任何布局/错误的布局(setContentView)。至于布局,我使用上面所示的“layoutVertical”。同时在xml中也有一个线性布局!!这样做对吗?
TableLayout
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
TableRow
Button
Button
Button
Button
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);