简体   繁体   中英

Android: Create special RadioButton programmatically

I am trying to create a kind of scale view. Therefore I use RadioButtons with the text on top of the RadioButton.

Defining this in my layout file works fine:

<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>

The result looks like this:

在此处输入图片说明

Now I only need the RadioGroup . I want to create the RadioButtons programmatically and add them to the RadioGroup.

I tried this code, but it does not work:

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);

I think SetButtonDrawable and/or SetCompoundDrawable are not the same as android:button and android:drawableBottom .

EDIT: Using just the code, without creating RadioButtons in the .axml file, I just get the text " Test " without a RadioButton.

在此处输入图片说明

EDIT 2:

Using SetCompoundDrawablesWithIntrinsicBounds I see the RadioButton but the Text is not centered.

在此处输入图片说明

EDIT 3:

Okay, with Mike's comments (edited in the code) it shows correctly one radio button

在此处输入图片说明

Now the next problem:

Since i need to add more than 1 radio button i tried a simple loop with the same code above.

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

the problem is, it is still only one RadioButton shown. The only thing that happens is, that there appears a little space between the RadioButton an the text.

在此处输入图片说明

EDIT 4:

With LayoutParams.WrapContent I am able to show all RadioButtons. But my intension was, that the RadioButtons fill up the RadioGroup. That's why I used match_parent as width and thought with weight=1 each RadioButton will get the same space.

在此处输入图片说明

EDIT 5:

Setting the width of the LayoutParameters to 0 does not work inside the loop where i create the RadioButtons. If I do so, there are no Radio Buttons displayed.

What works: After creating the RadioButtons in the for-loop I call this code:

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;
}

My only idea to explain this behavior is, that the RadioGroup does not know how many views it will hold and therefore it can't handle the weight attribute for an element that is just being inflated.

But thank you Mike to find this solution step by step!

I think you forgot to set the layout params to your radio button. Just add rb.setLayoutParams(rbParams); before adding your rb to your radioGroup

Solution that works for me:

    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;
    }

Simplified RadioGroup in .axml:

    <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM