设置运行时创建的radiobuttons之间的间隙/边距

时间:2016-06-13 14:01:39

标签: android android-radiobutton

我在代码中创建自定义radiobuttons。这是自定义的radioButton类

public class SurveyRadioButton extends RadioButton {

private int value;
private int questionId;

public SurveyRadioButton(Context context) {
    super(context);

    setBackgroundResource(R.drawable.selector_checkbox);

    setButtonDrawable(new StateListDrawable());

    setPadding(10, 20, 10, 20);

    setTextSize(17);

}


public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

public int getQuestionId() {
    return questionId;
}

public void setQuestionId(int questionId) {
    this.questionId = questionId;
}

}

然后创建脚本

LinearLayout.LayoutParams matchWrapLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);


LinearLayout root = (LinearLayout) findViewById(R.id.root);

    RadioGroup radioGroup = new RadioGroup(SplashActivity.this);

    radioGroup.setLayoutParams(matchWrapLayoutParams);

    {
        for (int ii = 0; ii < 5; ii++) {


            SurveyRadioButton radioButton = new SurveyRadioButton(this);

            radioButton.setLayoutParams(matchWrapLayoutParams);

            radioButton.setText("arda");

            radioButton.setSelected(false);

            radioGroup.addView(radioButton);
        }

        root.addView(radioGroup);

    }

这些代码会产生视图,但我想在radiobuttons之间添加间隙。将边距设置为布局参数不起作用。我想知道会是什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

我无法找到任何聪明的解决方案。但是将空白视图添加到radiogroup可以解决这个问题。

        SurveyRadioButton radioButton = new SurveyRadioButton(this);

        radioButton.setLayoutParams(matchWrapLayoutParams);

        radioButton.setText("arda");

        radioButton.setSelected(false);

        radioGroup.addView(radioButton);

        //Above 4 rows for blank view
        View blankView = new View(QuestionActivity.this);

        LinearLayout.LayoutParams matchWrapLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 20);

        blankView.setLayoutParams(matchWrapLayoutParams);

        radioGroup.addView(blankView);