Programmatically-added RadioButtons refuse to obey LayoutParams weighting

前端 未结 3 1056
余生分开走
余生分开走 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:00

    FYI, to do it without any xml at all

        RadioGroup rgrp = new RadioGroup(context);
        rgrp.setLayoutParams(new RadioGroup.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        rgrp.setOrientation(LinearLayout.HORIZONTAL);
    
            mAccent = new RadioButton(context);
            mAccent.setText("Accent");
            mAccent.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
            rgrp.addView(mAccent);
    
            mGhost = new RadioButton(context);
            mGhost.setText( "Ghost");
            mGhost.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
            rgrp.addView(mGhost);
    
            mFlam = new RadioButton(context);
            mFlam.setText( "Flam");
            mFlam.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
            rgrp.addView(mFlam);
    
        layout.addView(rgrp);
    

提交回复
热议问题