2013-04-25 58 views
1

我試圖居中tablerow的內部一個TextView,這本身就是一個tablelayout裏面,但我想我失去了一些東西,因爲TextView中沒有得到居中:■中心的TextView編程上的TableRow

這裏我的方法:

private void insertClock() { 


     TableLayout table = new TableLayout(this); 
     table.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     table.setStretchAllColumns(true); 

     TableRow tbRow = new TableRow(this); 
     TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 




     clock = new TextView(this); 
     TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); 


     clock.setLayoutParams(layoutHistory); 
     clock.setText(calculateClockText()); 
     tbRow.setLayoutParams(layoutRow); 
     table.addView(tbRow); 
     clock.setGravity(Gravity.CENTER_HORIZONTAL); 
     tbRow.addView(clock); 

    } 

在此先感謝;)

回答

5

Finnaly做它的工作! 以下是解決方案:

private void insertClock(TableLayout table, String text) { 

     TableRow tbRow = new TableRow(this); 
     TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); 


     tbRow.setLayoutParams(layoutRow); 

     clock = new TextView(this); 
     TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); 
     clock.setLayoutParams(layoutHistory); 
     clock.setText(text); 


     clock.setGravity(Gravity.CENTER); 
     tbRow.setGravity(Gravity.CENTER); 
     tbRow.addView(clock); 
     table.addView(tbRow); 
    }