1

我不明白为什么,在这段代码中,TableRow 出现在左上角而不是中心

HorizontalScrollView HSC = new HorizontalScrollView(this);
ScrollView VSC = new ScrollView(this);
HSC.setBackgroundResource(R.drawable.lavagna);   
tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tableLayout.setGravity(Gravity.CENTER)  
for (int i = 0; i < 3; i++){  
tableRow = new TableRow(this);  
tableRow.setGravity(Gravity.CENTER);  
for (int j = 0; j < 3; j++){  
values[i][j] = new EditText(this);   
values[i][j].setHint("r: " + (i) + " " + "c: " + (j));  
values[i][j].setPadding(10, 10, 10, 10);  
tableRow.addView(values[i][j]);  
}  
tableLayout.addView(tableRow);  
}  
VSC.addView(tableLayout);  
HSC.addView(VSC);  
setContentView(HSC);  
}  
4

2 回答 2

1
HorizontalScrollView HSC = new HorizontalScrollView(this);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,  LayoutParams.MATCH_PARENT, Gravity.CENTER));         
HSC.setBackgroundResource(R.drawable.lavagna);        
tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));  
values = new EditText[15][15];
for (int i = 0; i < 15; i++){           
tableRow = new TableRow(this); 
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < 15; j++){
values[i][j] = new EditText(this);
valo[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
values[i][j].setHint("r: " + (i) + " " + "c: " + (j));
values[i][j].setPadding(10, 10, 10, 10);
tableRow.addView(valore[i][j]);
}  
VSC.addView(tableLayout);
HSC.addView(VSC);
setContentView(HSC);         
于 2012-04-25T07:37:38.290 回答
0

我认为在初始化tableLayout对象之后,你还应该定义它的布局参数。

tableLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
于 2012-04-25T07:02:44.180 回答