我正在尝试创建一个大小为 2*X 的表,并且我希望列的宽度相等。我如何做到这一点?我已经尝试过 LinearLayout 和 tabelLayout,只要每个单选按钮上的文本相同,它们都可以正常工作,但是当不是这种情况时,它们会不均匀地分割空间(即使文本有空间并且相等分割) . 我曾考虑在运行时设置宽度,但我的 getWidth() 返回 0。非常感谢帮助。
附言。我没有使用 XML
代码:
imports....
public class SpendoRadioGroup extends LinearLayout implements OnCheckedChangeListener{
private GUI_attrs gui_attrs;
private RadioButton[] buttons;
private RadioButton selected = null;
public SpendoRadioGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    gui_attrs = new GUI_attrs(context, attrs);
    this.setBackgroundColor(gui_attrs.color_Z1);
    this.setOrientation(LinearLayout.VERTICAL);
    this.setPadding(gui_attrs.padding_Z1, gui_attrs.padding_Z1/2, gui_attrs.padding_Z1, gui_attrs.padding_Z1/2);
    RadioButton[] btns = new RadioButton[2];
    btns[0] = new RadioButton(context);
    btns[0].setText("Item 1");
    btns[1] = new RadioButton(context);
    btns[1].setText("Itemes 2");
    this.setRadioButtons(btns, "Instruction:");
}
public void setRadioButtons(RadioButton[] buttons, String instruction){
    this.removeAllViews();
    TextView tvInstruction = new  TextView(gui_attrs.context);
    tvInstruction.setTextColor(Color.BLACK);
    tvInstruction.setText(instruction);
    tvInstruction.setTextSize(gui_attrs.textSize_tiny);
    this.addView(tvInstruction);
    this.buttons = buttons;
    LinearLayout.LayoutParams param = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    param.weight = 1;
    for(int i = 0; i < buttons.length; i++){
        buttons[i].setBackgroundColor(gui_attrs.color_Z2);
        buttons[i].setLayoutParams(param);
        buttons[i].setOnCheckedChangeListener(this);
    }
    LinearLayout row;
    for(int i = 0; i < buttons.length/2; i++){
        row = new LinearLayout(gui_attrs.context);
        row.setOrientation(LinearLayout.HORIZONTAL);
        row.setBackgroundColor(gui_attrs.color_Z1);
        if(i == 0){
            row.setPadding(0, 0, 0, gui_attrs.padding_Z1/2);
        }else{
            row.setPadding(0, gui_attrs.padding_Z1/2, 0, gui_attrs.padding_Z1/2);
        }
        row.addView(buttons[i*2]);
        TextView tvDiv = new TextView(gui_attrs.context);
        tvDiv.setPadding(gui_attrs.padding_Z1, 0, 0, 0);
        tvDiv.setBackgroundColor(gui_attrs.color_Z1);
        row.addView(tvDiv);
        row.addView(buttons[i*2+1]);
        this.addView(row);
    }
    if(buttons.length%2 > 0){
        row = new LinearLayout(gui_attrs.context);
        row.setBackgroundColor(gui_attrs.color_Z1);
        row.setPadding(gui_attrs.padding_Z1, gui_attrs.padding_Z1/2, gui_attrs.padding_Z1, gui_attrs.padding_Z1/2);
        param = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        param.weight = 0.8f;
        TextView tvDiv = new TextView(gui_attrs.context);
        tvDiv.setLayoutParams(param);
        row.addView(tvDiv);
        buttons[buttons.length-1].setGravity(Gravity.CENTER);
        row.addView(buttons[buttons.length-1]);
        tvDiv = new TextView(gui_attrs.context);
        tvDiv.setLayoutParams(param);
        row.addView(tvDiv);
        this.addView(row);
    }
}
@Override
public void onCheckedChanged(CompoundButton view, boolean checked) {
    if(checked){
        for(int i = 0; i < buttons.length; i++){
            if(!buttons[i].equals(view)){
                buttons[i].setChecked(false);
            }else{
                selected = buttons[i];
            }
        }
    }
}
public void createRadioButtons(String[] text, String instruction){
    RadioButton[] buttons = new RadioButton[text.length];
    for(int i = 0; i < text.length; i++){
        buttons[i] = new RadioButton(gui_attrs.context);
        buttons[i].setText(text[i]);
    }
    this.setRadioButtons(buttons, instruction);
}
public String getSelected(){
    return selected.getText().toString();
}
///Failed attempt...
@Override
protected void onFinishInflate (){
    super.onFinishInflate();
    System.out.println("Finnish inflate: " + this.getWidth());
}
}