Java 如何设置动态创建的表格布局列(单元格)的宽度和高度
在我的项目中,我动态地创建表。每个表格都是大学生每周的日程安排。因此,每个单元格中都有一个字符串。我的工作代码如下Java 如何设置动态创建的表格布局列(单元格)的宽度和高度,java,android,border,cell,tablelayout,Java,Android,Border,Cell,Tablelayout,在我的项目中,我动态地创建表。每个表格都是大学生每周的日程安排。因此,每个单元格中都有一个字符串。我的工作代码如下 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; TableLayout.LayoutParam
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(width/9,height/12);
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for(int m=0;m<major.size();m++) {
for (int i = 0; i <rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j = 0; j <columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
WeeklySchedule ws=major.get(m);
String course;
if (i == 0 && j == 0) {
textView.setText(" ");
} else if (i == 0) {
textView.setText(cv[j-1]);
textView.setBackgroundColor(Color.GREEN);
} else if (j == 0) {
textView.setText(rv[i -1]); textView.setBackgroundColor(Color.GRAY);
} else if(j!=0&&i!=0){
if(ws.table[i-1][j-1].size()==2){
course=ws.table[i-1][j-1].get(0).getCourseCode()+"/"+ws.table[i-1][j-1].get(1).getCourseCode();
textView.setBackgroundColor(Color.RED);
}
else if(ws.table[i-1][j-1].size()==0){
course= " ";
}
else {
course=ws.table[i-1][j-1].get(0).getCourseCode();
}
textView.setText(course);
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
Display Display=getWindowManager().getDefaultDisplay();
点大小=新点();
display.getSize(size);
int width=size.x;
整数高度=大小y;
TableLayout.LayoutParams tableLayoutParams=新建TableLayout.LayoutParams();
TableLayout TableLayout=新的TableLayout(本);
tableLayout.setBackgroundColor(颜色:黑色);
//2)创建tableRow参数
TableRow.LayoutParams tableRowParams=新的TableRow.LayoutParams(宽/9,高/12);
tableRowParams.setMargins(1,1,1,1);
tableRowParams.weight=1;
对于(int m=0;m