2014-01-08 23 views
1

enter image description here添加無線電組到表LAyout,android?

這是我想創建但通過java代碼動態創建的佈局。 這裏的父佈局是LinearLayout。

通過java代碼創建的tableLayout包含5個動態創建的單選按鈕。

我需要將這些單選按鈕放在收音機組中。 如何做到這一點。

到目前爲止,我試過這是工作正常。但我無法在1個收音機組中添加所有5個單選按鈕。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    l = (LinearLayout) findViewById(R.id.mainl); //parent layout 

    t1 = new TableLayout(this); 

    rb1 = new RadioButton(this); 
    rb2 = new RadioButton(this); 
    rb3 = new RadioButton(this); 

    t1.setLayoutParams(new TableLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    TableRow row = new TableRow(this); 
    TableRow row1 = new TableRow(this); 

    row.addView(rb1); 
    row.addView(rb2); 
    row1.addView(rb3); 

    t1.addView(row); 
    t1.addView(row1); 
    l.addView(t1); 

} 

回答

0

我找不到你的RadioGroup的聲明。

你需要的東西是這樣的:從LinearLayout

... 

RadioGroup rg = new RadioGroup(this); 
rg.setOrientation(RadioGroup.HORIZONTAL); 

rg.addView(rb1); 
rg.addView(rb2); 
rg.addView(rb3); 

row.addView(rg); 

... 
+0

擴展,這不是我想要的,在第一排2個單選按鈕,在第2行2個單選按鈕,在第3排1單選按鈕。將所有這些單選按鈕添加到廣播組 – WISHY

0

RadioGroup繼承。您將不得不創建自己的實現RadioGroup,它繼承自TableLayout

如果您檢查source codeRadioGroup,您會看到它只是跟蹤其子視圖RadioButton's並確保只檢查其中一個。除此之外,它提供了一個監聽器,如果檢查RadioButton的變化。

您可以嘗試修改源,使其從TableLayout