2012-02-11 63 views
0

這裏是我的代碼顯示單選按鈕2行3列編程中的Android

final RadioButton[] rb = new RadioButton[6]; 
     RadioGroup rg = new RadioGroup(this); 
     rg.setOrientation(RadioGroup.HORIZONTAL); 

     for(int i=0; i<6; i++){ 
      rb[i] = new RadioButton(this); 
      rb[i].setText("Dynamic Radio Button " + i); 
      rb[i].setId(i); 
      rb[i].getGlobalVisibleRect(null); 
      rg.addView(rb[i]); 
      } 
      ll.addView(rg); 

我想以編程方式顯示這些單選按鈕。我有6個單選按鈕,但無法將它們顯示爲行和列。

回答

0

您可以使用TableLayout行和列

示例代碼

LinearLayout ll = new LinearLayout(this); 
    TableLayout tl = new TableLayout(this); 
    for (int i = 0; i < 3; i++) { 
     TableRow tr = new TableRow(this); 
     for (int j = 0; j < 6; j++) { 
      RadioButton rb = new RadioButton(this); 
     rb.setText("Dynamic Radio Button " + i); 
     rb.setId(i); 
     rb.getGlobalVisibleRect(null); 
     tr.addView(rb); 
     } 
     tl.addView(tr); 
    } 
ll.addView(tl); 
setContentView(ll); 

Screenshots

Screenshots link

+0

感謝您的回覆,安排的單選按鈕。但我需要單選按鈕 – vimalatha 2012-02-11 12:46:01