动态单选按钮未正确对齐

时间:2014-02-17 05:46:54

标签: android

enter image description here动态线性布局权重未正确设置,请参阅以下编码

                LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
                        1.0f);
                mainparams.setMargins(0, 10, 0, 0);
                LinearLayout li = new LinearLayout(Appointmentdetails.this);
                li.setOrientation(LinearLayout.HORIZONTAL);

                li.setLayoutParams(mainparams);

                LinearLayout text = new LinearLayout(Appointmentdetails.this);
                text.setWeightSum(1);
                LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                        2.0f);
                text.setLayoutParams(lparams);
                tv = new TextView(this);
                tv.setTextSize(18);
                tv.setTypeface(MyriadPro);
                tv.setLayoutParams(lparams);
                tv.setText(Alldata.question.get(i).getgQuestion());
                tv.setSingleLine();
                tv.setPadding(25, 0, 0, 0);
                tv.setMarqueeRepeatLimit(10);
                tv.setEllipsize(TruncateAt.MARQUEE);
                tv.setSelected(true);
                text.addView(tv);
                li.addView(text);

                LinearLayout Spi = new LinearLayout(Appointmentdetails.this);
                Spi.setWeightSum(1);
                LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                        1f);
                Spi.setLayoutParams(lparams1);
                RadioGroup rg = new RadioGroup(Appointmentdetails.this);

                // rg.setLayoutParams(lparams1);

                gAnswerList.add("Select");
                for (int j = 0; j < helptext.split(Expressions).length; j++) {

                    RadioButton rb = new RadioButton(Appointmentdetails.this);
                    rb.setText(helptext.split(Expressions)[j].toString());
                    rb.setId(j);
                    rb.setLayoutParams(lparams1);
                    rg.addView(rb);

                }

                rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup rg, int checkedId) {
                        for (int i = 0; i < rg.getChildCount(); i++) {
                            RadioButton rb = (RadioButton) rg.getChildAt(i);
                            if (rb.getId() == checkedId) {
                                CharSequence text = rb.getText();
                                mSelectedRadioValue = text + ",";
                                gAnswerList.set(i2, text.toString());
                                // do something with text
                                return;
                            }
                        }
                    }
                });
                Spi.addView(rg);
                li.addView(Spi);
                mQuestionLayout.addView(li);

            }
}

这就是我在动态线性布局中添加一个单选按钮的方式,我不知道我的编码错误,它没有正确对齐,请建议我得到解决方案。提前谢谢。

2 个答案:

答案 0 :(得分:1)

我会尝试将布局参数设置为居中。你几乎只是给他们自由流动和设置WRAP_CONTENTS他们喜欢的地方。是的,他们不会填满整个屏幕,这不是你想要的,但尝试通过像尝试做这样的事情

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    2.0f, lparams.Gravity.CENTER);

答案 1 :(得分:1)

尝试使用单选按钮和文本对齐

LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT
           );
      LinearLayout li = new LinearLayout(Appointmentdetails.this);
      li.setOrientation(LinearLayout.HORIZONTAL);

      li.setLayoutParams(mainparams);
      li.setWeightSum(2);

      LinearLayout.LayoutParams mainparams1 = new LinearLayout.LayoutParams(
              0, LayoutParams.WRAP_CONTENT
             );
      mainparams1.weight = 1;
      TextView tv = new TextView(this);
      tv.setTextSize(18);
      tv.setLayoutParams(mainparams1);
      tv.setText("text  ");


      RadioGroup rg = new RadioGroup(Appointmentdetails.this);
      rg.setLayoutParams(mainparams1);
      rg.setOrientation(RadioGroup.VERTICAL);


     for (int j = 0; j < helptext.split(Expressions).length; j++) {
 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
                 );
                RadioButton rb = new RadioButton(Appointmentdetails.this);
                rb.setText(helptext.split(Expressions)[j].toString());
                rb.setId(j);
                rb.setLayoutParams(params );
                rg.addView(rb);

            }
      li.addView(tv);
      li.addView(rg);