为什么TableRow不在屏幕中心?

时间:2012-04-25 06:56:34

标签: android android-layout

我不明白为什么,在这段代码中,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);  
}  

2 个答案:

答案 0 :(得分: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);         

答案 1 :(得分:0)

我认为在初始化 tableLayout 对象后,您还应该定义其布局参数。

tableLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));