首页 > 解决方案 > Android:以编程方式创建特殊的 RadioButton

问题描述

我正在尝试创建一种比例视图。因此,我将 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);

我认为SetButtonDrawable和/或SetCompoundDrawableandroid:button和不同android:drawableBottom

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

在此处输入图像描述

编辑2:

使用 SetCompoundDrawablesWithIntrinsicBounds 我看到 RadioButton 但文本未居中。

在此处输入图像描述

编辑 3:

好的,随着迈克的评论(在代码中编辑),它正确显示了一个单选按钮

在此处输入图像描述

现在下一个问题:

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

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

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

在此处输入图像描述

编辑4:

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

在此处输入图像描述

编辑 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 不知道它将拥有多少视图,因此它无法处理刚刚被膨胀的元素的权重属性。

但是感谢迈克一步一步地找到这个解决方案!

标签: c#androidxamarin.androidradio-buttonandroid-radiobutton

解决方案


适合我的解决方案:

    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>

推荐阅读