2014-02-17 130 views
0

enter image description here動態的LinearLayout重量沒有設置正確,請參閱下面的編碼動態單選按鈕沒有正確對齊

   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); 

      } 
} 

這是我如何加入動態線性佈局內的單選按鈕,我不知道什麼是錯在我的編碼中它沒有正確對齊,請建議我獲得解決方案。提前致謝。

+0

帖子實現和你現在正在得到什麼。 – Sonali8890

+0

嗨檢查我的屏幕截圖,我ahve使用框到我的textview,並增加了一些重量的textview linearlayout,bt如果我添加了一個textview與長文本我的單選按鈕沒有正確配置, – Madhu

回答

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); 
+0

非常感謝,它的工作完美。 – Madhu

1

我會嘗試設置佈局參數居中。您幾乎可以通過WRAP_CONTENTS自由流動並設置他們喜歡的任何位置。是的,他們不會填滿是不是你想要的東西了整個屏幕,而是試圖通過類似嘗試做這樣的事情

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