-3
您好我想添加一些TableRows到TableLayout,TableLayout是在xml文件中声明的,TableRows只是在java代码中进行了定义。 Altough也有一些类似的问题,我不能让它在我的代码工作:如何以编程方式将表格行添加到表格布局?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
public class MainActivity extends Activity {
TableLayout tableLayout;
Button[][] buttons =new Button[9][9];
TableRow tableRow[]=new TableRow[9];
TableRow.LayoutParams layoutParams=new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tableLayout = (TableLayout) findViewById(R.id.tablay);
for(int line=0; line<9; line++){
tableRow[line]=new TableRow(this);
tableRow[line].setLayoutParams(layoutParams);
tableRow[line].setVisibility(View.VISIBLE);
}
for(int line=0; line<9; line++){
for(int col=0; col<9; col++){
buttons[line][col]=new Button(this);
buttons[line][col].setText(""+line+col);
buttons[line][col].setLayoutParams(layoutParams);
buttons[line][col].setVisibility(View.VISIBLE);
tableRow[line].addView(buttons[line][col]);
}
}
for(int i=0; i<9; i++){
tableLayout.addView(tableRow[i], new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(R.layout.activity_main);
}
}
的应用始终发射后立即终止,我看不出有什么问题。 感谢您的帮助!
非常感谢!现在它的工作 – Jonas1902
伟大的,投票和接受答案然后:) – Pehlaj
我接受了答案,也upvoted但upvote不会公开显示,因为我没有足够的声誉 – Jonas1902