2013-06-21 37 views
0

在我的代碼動態創建行,對於一些行,我需要TextView填滿整個行,而不是第一個單元格,即:TextView的灌裝整行編程

-------------------- 
| Welcome  | 
|------------------| 
|cell1|cell2|cell3 | 
|------------------| 
|cell1|cell2|cell3 | 
-------------------- 

我的代碼:

TableRow welcome_row= new TableRow(getBaseContext()); 
TextView welcome= new TextView(getBaseContext()); 
welcome.setText("Welcome"); 
table.addView(welcome_row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

我該如何製作歡迎Textview填滿整行?

+0

請參閱本http://stackoverflow.com/questions/15590473/programmatically-adding-rows-to-tablelayout-leads-to-incorrect-view-hierarchy – Riser

回答

0

試試這個

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TableLayout tableLayout = new TableLayout(this); 
     tableLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 

     TableRow welcome_row = new TableRow(this); 
     welcome_row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
     welcome_row.setGravity(Gravity.CENTER); 
     welcome_row.setBackgroundColor(Color.GRAY); 

     TextView welcome = new TextView(this); 
     welcome.setText("Welcome!"); 
     welcome.setBackgroundColor(Color.GREEN); 

     // add textview to table row 
     welcome_row.addView(welcome); 

     // add table row to table 
     tableLayout.addView(welcome_row); 

     setContentView(tableLayout); 
    } 
}