Android: 以编程方式创建特殊的 RadioButton

Android: Create special RadioButton programmatically

我正在尝试创建一种比例视图。因此,我将 RadioButtons 与 RadioButton 顶部的文本一起使用。

在我的布局文件中定义它工作正常:

<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Text on top"
            android:button="@null"
            android:drawableBottom="@android:drawable/btn_radio"
            android:gravity="center"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Text on top"
            android:button="@null"
            android:drawableBottom="@android:drawable/btn_radio"
            android:gravity="center"
            android:layout_weight="1"/>
    </RadioGroup>
</LinearLayout>

结果如下所示:

现在我只需要 RadioGroup。我想以编程方式创建 RadioButtons 并将它们添加到 RadioGroup。

我试过这段代码,但它不起作用:

RadioButton rb = new RadioButton(Context);
LinearLayout.LayoutParams rbParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
rb.SetButtonDrawable(null);
rb.SetCompoundDrawablesWithIntrinsicBounds(null,null,null, Resources.GetDrawable(Android.Resource.Drawable.ButtonRadio));
rb.Text = "Test";
rb.Gravity = GravityFlags.Center;
rbParams.Weight = 1;
rb.LayoutParameters = rbParams;
radioGroup.AddView(rb);

我认为SetButtonDrawableand/orSetCompoundDrawableandroid:buttonandroid:drawableBottom不一样。

编辑: 仅使用代码,而不在 .axml 文件中创建 RadioButtons,我只得到没有 RadioButton 的文本“Test”。

编辑 2:

使用 SetCompoundDrawablesWithIntrinsicBounds 我看到了 RadioButton,但文本没有居中。

编辑 3:

好的,根据 Mike 的评论(在代码中编辑)它正确显示了一个单选按钮

现在下一个问题:

因为我需要添加超过 1 个单选按钮,所以我尝试使用上面相同的代码进行简单循环。

   for (int i = 0; i < 4; i++)
  {
        RadioButton rb = new RadioButton(Context);
        ...
        radioGroup.AddView(rb);
   }

问题是,它仍然只显示一个 RadioButton。唯一发生的事情是,在 RadioButton 和文本之间出现了一点 space。

编辑 4:

使用 LayoutParams.WrapContent 我可以显示所有 RadioButton。但我的意图是,RadioButton 填满 RadioGroup。这就是为什么我使用 match_parent 作为宽度并认为 weight=1 每个 RadioButton 将获得相同的 space.

编辑 5:

将 LayoutParameters 的宽度设置为 0 在我创建 RadioButtons 的循环中不起作用。如果我这样做,则不会显示任何单选按钮。

什么有效: 在 for 循环中创建 RadioButtons 后,我调用此代码:

var count = radioGroup.ChildCount;

for (int i = 0; i < count; i++)
{
    var currentRb = radioGroup.GetChildAt(i);

    LinearLayout.LayoutParams rbParams2 = new LinearLayout.LayoutParams(0,
        ViewGroup.LayoutParams.WrapContent);
    rbParams2.Weight = 1;

    currentRb.LayoutParameters = rbParams2;
}

我对这种行为的唯一解释是,RadioGroup 不知道它将容纳多少视图,因此它无法处理刚刚膨胀的元素的权重属性。

但是谢谢你Mike一步步找到这个解决方案!

我想你忘记了为你的单选按钮设置布局参数。只需在将 rb 添加到 radioGroup

之前添加 rb.setLayoutParams(rbParams);

适合我的解决方案:

    foreach (var option in options)
    {
        RadioButton rb = new RadioButton(Context);
        rb.SetButtonDrawable(null);
        rb.SetCompoundDrawablesWithIntrinsicBounds(null,null,null,ContextCompat.GetDrawable(Context, Android.Resource.Drawable.ButtonRadio));
        rb.Text = option.Text;
        rb.Gravity = GravityFlags.Center;
        radioGroup.AddView(rb);
    }

    // Weight für die RBs setzen
    for (int i = 0; i < radioGroup.ChildCount; i++)
    {
        var currentRb = radioGroup.GetChildAt(i);
        LinearLayout.LayoutParams rbParams = new LinearLayout.LayoutParams(0,
            ViewGroup.LayoutParams.WrapContent) {Weight = 1};
        currentRb.LayoutParameters = rbParams;
    }

.axml 中的简化 RadioGroup:

    <LinearLayout
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:gravity="center"
    android:layout_width="match_parent"
    android:id="@+id/formItemScaleLayout"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

        <TextView
            android:id="@+id/formItemScaleStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"/>

        <RadioGroup
            android:layout_weight="1"
            android:id="@+id/formItemScaleRadioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </RadioGroup>

        <TextView
            android:id="@+id/formItemScaleEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="End"/>
</LinearLayout>