2011-03-27 25 views
3

我的佈局看起來像這樣如何動態地添加單選按鈕,而不是失去瀏覽內容

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:text="Please select the restaurant you want to rate: " /> 

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

     </RadioGroup> 
     <Button android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text="Rate it" 
     android:id="@+id/submitSelection"/> 
</LinearLayout> 

當我在Java文件中添加單選按鈕到RadioGroup中,單選按鈕重疊彼此並提交按鈕消失。我猜測單選按鈕正在壓倒其他觀點。我如何動態添加這些單選按鈕而不會丟失其他元素。

的Java代碼如下所示:

setContentView(R.layout.submit_select); 
     RadioGroup radioGroup = (RadioGroup)findViewById(R.id.SelectRestaurant); 
     for (String restaurant : restaurants) { 
      RadioButton radioButton = new RadioButton(getBaseContext()); 
      radioButton.setText(restaurant); 
      radioGroup.addView(radioButton); 
     } 
     radioGroup.invalidate(); 
     dismissDialog(DIALOG_SUBMIT_ID); 
     rateItButton = (Button) findViewById(R.id.submitSelection); 
     rateItButton.setOnClickListener(this); 
+1

「單選按鈕互相重疊,提交按鈕不見了「 - 這有點不清楚,你可以發佈它的截圖嗎?我想因爲你沒有使用'ScrollView',所以按鈕會消失,因爲它會被擠出屏幕的可見部分。然而,沒有截圖,這只是一個猜測。 – 2011-03-27 21:25:23

回答

2

嘗試用WRAP_CONTENT的寬度和高度創建佈局參數,然後添加的LayoutParams當您添加的按鈕組:

radiogroup.addView(newRadioButton, layoutParams); 
相關問題