Programmatically-added RadioButtons refuse to obey LayoutParams weighting

前端 未结 3 1082
余生分开走
余生分开走 2020-12-30 01:17

I\'m trying to create a RadioGroup within an Android layout where the child RadioButtons are stretched to evenly fill the entire width of the

3条回答
  •  太阳男子
    2020-12-30 02:03

    When you set a layout weight, you should use fill_parent as layout width. Then you shouldn't use LinearLayout.LayoutParams but RadioGroup.LayoutParams, as you're adding radio buttons to a RadioGroup, not to a simple LinearLayout.

    Finally, as you use the inflater to "build" the radio button, the XML file of the radio button already has the layout params picked from the XML file, so I think you should just call the addView method that takes only the view to add as parameter (that is addView(View v)) and change the layout_width to fill_parent.

    Note that, if you'll need to reference the variable "button" in the code, i.e. add a click listener, you'll add the listener only to the last created button. You'll have to create a RadioButton object for each RadioButton you will add to the RadioGroup (button, button1, button2, etc).

提交回复
热议问题