2014-11-23 52 views
1

我的佈局XML是什麼樣子的Android RadioGroup中水平對齊的內容不可見

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollview1" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
android:id="@+id/linearlayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 


</LinearLayout> 

</ScrollView> 

我創造RadioGroup中和

LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);   
     RadioGroup rg = new RadioGroup(this); 
     rg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
     rg.setOrientation(0); 
     while(itr.hasNext()){ 
      final String drinkname = itr.next(); 
      System.out.println("drinkname: "+drinkname); 
      RadioButton rb = new RadioButton(this); 
      rb.setText(drinkname);    
      rg.addView(rb); 
     } 
     rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       //group.clearCheck(); 

       int id = group.getCheckedRadioButtonId(); 
       RadioButton selectrb = (RadioButton) findViewById(id); 
       System.out.println(selectrb.getText().toString()); 
       WebMenu.drinkname = selectrb.getText().toString(); 


      } 
     }); 
     ll.addView(rg); 

它應該表現出6個值通過代碼添加單選按鈕;但它只顯示3個,剩餘看起來像隱藏。有人可以幫我嗎?

在此先感謝!

+0

請問您可以添加圖片嗎? – joao2fast4u 2014-11-23 20:55:15

+0

你的代碼工作正常。你確定你的迭代器有6個元素嗎? – joao2fast4u 2014-11-23 21:11:21

+0

@ joao2fast4u - SO不允許添加圖片。我已經加入了保管箱。請找到鏈接。 https://www.dropbox.com/s/kju5uawmsfxhome/SO1.png?dl=0 – Prabhu 2014-11-23 22:04:01

回答

0

隨着目前的RadioGroup類的實施,你不能有兩行RadioButtons。 因此,您可以將HorizontalScrollView設置爲佈局,並允許用戶滾動查看所有RadioButtons或使用兩個RadioGroups,其中每個包含3個RadioButtons

所以,使用例如(很少變化),這最後的解決方案可以這樣實現:

LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);   
    RadioGroup rg = new RadioGroup(this); 
    RadioGroup rg2 = new RadioGroup(this); 
    rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    rg.setOrientation(LinearLayout.HORIZONTAL); 
    rg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    rg2.setOrientation(LinearLayout.HORIZONTAL); 
    String [] drinks = new String [] {"Mountain Dew", "7Up", "Root Beer", "Pepsi", "Cola", "Ice Tea"}; 
    int i = 0; 

    while(i<drinks.length){ 
     final String drinkname = drinks[i]; 
     Log.i(TAG, "drinkname: "+drinkname); 
     RadioButton rb = new RadioButton(this); 
     rb.setText(drinkname); 
     if(i %2==0) 
      rg.addView(rb); 
     else 
      rg2.addView(rb); 
     i++; 
    } 

    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      //unselect all radios in the other RadioGroup 
      if(rg2.getCheckedRadioButtonId()!=-1){ 
       rg2.setOnCheckedChangeListener(null); 
       rg2.clearCheck(); 
       rg2.setOnCheckedChangeListener(this); 
      } 

      int id = group.getCheckedRadioButtonId(); 
      RadioButton selectrb = (RadioButton) findViewById(id); 
      if(selectrb != null){ 
       Log.i(TAG,selectrb.getText().toString()); 
       WebMenu.drinkname = selectrb.getText().toString(); 
      } 


     } 
    }); 

    rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      //unselect all radios in the other RadioGroup 
      if(rg.getCheckedRadioButtonId()!=-1){ 
       rg.setOnCheckedChangeListener(null); 
       rg.clearCheck(); 
       rg.setOnCheckedChangeListener(this); 
      } 

      int id = group.getCheckedRadioButtonId(); 
      RadioButton selectrb = (RadioButton) findViewById(id); 
      if(selectrb != null){ 
       Log.i(TAG,selectrb.getText().toString()); 
       WebMenu.drinkname = selectrb.getText().toString(); 
      } 


     } 
    }); 

    ll.addView(rg); 
    ll.addView(rg2); 

你必須要小心的事實,你的兩個RadioGroups跟蹤的只有3 RadioButtons每。他們不互相溝通,也不互相影響。因此,如果您從第一行(第一行RadioGroup)中選擇一個RadioButton,您仍然可以在第二行RadioGroup上選擇,反之亦然。你必須自己管理這種情況。

編輯:來管理,這是取消選擇一種方式全部radioButtons在RadioGroup1,當我們選擇一個RadioGroup2和RadioButton,反之亦然。我已經添加了代碼來做到這一點。