将广播组添加到Table LAyout,android?

时间:2014-01-08 09:41:43

标签: android tablelayout

enter image description here

这是我想要创建的布局,但是通过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);

}

2 个答案:

答案 0 :(得分:0)

我找不到你对RadioGroup的声明。

你需要这样的东西:

...

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

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

row.addView(rg);

...

答案 1 :(得分:0)

RadioGroup继承自LinearLayout。您必须创建自己的继承自TableLayout

的RadioGroup实现

如果您检查RadioGroup的{​​{3}},您会看到它只是跟踪RadioButton的子视图,并确保只检查其中一个。{1}}。除此之外,如果检查RadioButton更改,它会提供一个监听器。

您可以尝试修改源代码,使其从TableLayout等扩展。