【问题标题】:Radio Button Set Margins and Alignment Programmatically单选按钮以编程方式设置边距和对齐
【发布时间】:2018-12-01 15:48:35
【问题描述】:

我正在使用 RadioButtons 设置 RadioGroup,它们都是根据 Firebase 中的数据动态填充的。我设置的参数如下:

 mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    mParams.setMargins(0,0,4,6);

然后我实例化我的 RadioButton 对象:

public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
        mPollAnswerArrayList = new ArrayList<RadioButton>();
        int indexCreated = 0;
        for (DocumentSnapshot answerSnapshot : answers) {
            Answer answer = answerSnapshot.toObject(Answer.class);
            mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
            RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
            radioButton.setTag(indexCreated);
            radioButton.setText(answer.getAnswer().toString());
            radioButton.setTextColor(getResources().getColor(R.color.black));
            radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
            //TODO: Investigate if this line is necessary
            radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

            mPollQuestionRadioGroup.addView(radioButton, mParams);
            indexCreated++;
        }
    }

下面是它的外观。我想在单选按钮和文本之间添加空格。我还想要每个按钮之间的空间。最后,如果可能的话,我希望文本以按钮为中心。

【问题讨论】:

    标签: android xml radio-button


    【解决方案1】:

    这是在按钮和文本之间添加空格的简单方法:

    radioButton.setText("   " + answer.getAnswer().toString());
    

    要改变按钮之间的距离,你可以这样做:

    LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
    );
    params.setMargins(leftMargin, topMargin, rightMargin, 15);
    radioButton.setLayoutParams(params);
    

    15 是底部边距,您可以随意更改。我希望这会有所帮助。

    【讨论】:

    • 谢谢 - 知道如何将文本中心与按钮对齐吗?
    • 对不起,我不完全明白你的意思。在图片中,文字位于中间。
    • 现在,文本底部与按钮底部对齐。如果可以澄清的话,我希望文本的中间与按钮的中间对齐
    【解决方案2】:

    试试这个。

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
         RadioGroup radioGroup = new RadioGroup(getContext());
         RadioButton radioButton = new RadioButton(getContext());
         radioButton.setLayoutParams(params1);
         radioButton.setId(1);
         radioButton.setText("text");
         radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
         radioGroup.addView(radioButton);
    

    使用它来检查/选择单选按钮,

    radioGroup.check(3);  
    

    将此用于未选中的单选按钮,

    radioGroup.clearCheck();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-15
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多