RadioButton布局

时间:2012-05-13 18:08:22

标签: android

我正在尝试使用多个RadioButton创建一个RadioGroup。第一个RadioButton在xml-File中定义:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <RadioButton
            android:id="@+id/radioButtonFirst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:gravity="center" />


    </RadioGroup>

其余部分只能在运行时添加,因为所需的RadioButton数量可能会有所不同。

RadioButton rbNext = new RadioButton(this);
        RelativeLayout.LayoutParams paramsRb = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        paramsRb.addRule(RelativeLayout.BELOW, rbFirst.getId());
        paramsRb.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        paramsRb.addRule(RelativeLayout.ALIGN_BOTTOM, ivNext.getId());
        paramsRb.setMargins(0, 0, 20, 0);

        rbNext.setLayoutParams(paramsRb);
        rbNext.setGravity(Gravity.CENTER);

        rbNext.setId(i*10+1);

创建和添加RadioButtons工作得很好但是如何在RadioGroup的整个宽度上分发RadioButtons?

1 个答案:

答案 0 :(得分:0)

通过ID获取您的RadioGroup并将您的按钮添加为孩子。

(RadioGroup) radioGroup = (RadioGroup) findByViewId(radioGroup1);
radioGroup.addView(yourRadioButton);